#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
// create array of size 20
int arr[20];
int i;
cout<<"scores : ";
for( i = 0 ; i < 20 ; i++ )
{
// generate a random number i range 70 - 100 an add it to arr
arr[i] = rand() % 31 + 70;
cout<<arr[i]<<" ";
}
// store the total score
int total = 0;
// store the highest score
int highest = 0;
// calculate the total score and the highest score
for( i = 0 ; i < 20 ; i++ )
{
total += arr[i];
// if the current score is more than the highest score
if( arr[i] > highest )
highest = arr[i];
}
// calculate average
double average = (double)total / 20.0;
cout<<"\n ave : "<<average<<endl;
cout<<" high : "<<highest;
return 0;
}
#include<iostream> #include<cstdlib> using namespace std; int main() { // create array of size 20 int arr[20]; int i; cout << "scores : "; for (i = 0; i < 20; i++) { // generate a random number i range 70 - 100 an add it to arr arr[i] = rand() % 31 + 70; cout << arr[i] << " "; } // store the total score int total = 0; // store the highest score int highest = 0; // calculate the total score and the highest score for (i = 0; i < 20; i++) { total += arr[i]; // if the current score is more than the highest score if (arr[i] > highest) highest = arr[i]; } // calculate average double average = (double) total / 20.0; cout << "\n ave : " << average << endl; cout << " high : " << highest; return 0; }
#include<iostream> #include<cstdlib> using namespace std; int main() { // create array of size 20 ...
Arrays C++ #include <iostream> using namespace std; int main() { int tests[6]; // array declaration int sum = 0; float avg; //input test scores cout << " Enter " << 6 << " test scores: " << endl; for (int i = 0; i < 6; i++) { cout << "Enter Test " << i + 1 << ": "; cin >> tests[i]; } return 0; } Type...
Consider the following C++ program: #include <iostream> #include <cstdlib> using namespace std; int main int n =0; int i = 0; cout << "Please enter a strictly positive number:"; cin >> n if (n <= 0) exit(EXIT_FAILURE) while (n > 1) n-n/2; i << endl; cout"Output:" return 0; Answer the following questions: What is the output of the program for each of the following values of n: -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9? What does...
#include <iostream> using namespace std; int * newZeroArray(int size) { //your code here } int main() { int size = 10; int * A = newZeroArray(size); for(int i = 0; i < size; i++) cout << A[i] << " "; cout << endl; }Write a function that takes a size, creates a new array of that size with all zeros, and returns the array. If the size is not a valid size for an array, do not return a valid...
#include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0)); int number, guess, response, reply; int score = 0 number = rand() % 100 + 1; do { do { cout << "Enter your guess "; cin >> guess; score++; if (guess < number) cout << guess << " is too low! Enter a higher number. "; else if (guess > number) cout << guess << " is too high! Enter a lower number....
#include <iostream> using namespace std; int main(void) { int SIZE; cout<<"Enter the size of the array"<<endl; cin>>SIZE; int *numlist = new int[SIZE]; // Read SIZE integers from the keyboard for (int i = 0; i<SIZE; i++ ) { cout << "Enter value #" << i+1 << ": "; cin >> numlist[i]; } // Display the numbers in a reverse order for (int i = SIZE; i > 0; i--...
One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << ; cout << endl; return 0; }
Flow chart of this program #include <iostream> #include <cmath> using namespace std; int mainO double sum=0.0, ave-ee, int locmx, locmn int n; cout <<"Enter the number of students: "<<endl; cin >> ni double listln]; double max-0; double min-e; for (int i-e ; i<n ;i++) cout<s enter the gpa: "<cendli cin>>listli]; while (listlile i listli1>4) cout<s error,Try again "<cendl; cin>listlil: sun+=list[i]; N1 if (listli]>max) for(int isin itt) max=list [i]; 10cmx=1+1 ; else if (list [i] min) min=list [i]; locmn-i+1; ave sum/n;...
#include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be implemented by you } int main() { cout << "To calculate x^y ..." << endl; double x; int y; cout << "Please enter x: "; cin >> x; cout << "Please enter y: "; cin >> y; if(x == 0) { if (y > 0) cout << 0 << endl; else cout << "x^y is not defined" <<endl; } else { cout << improvedPow(x,y)...
Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){ int rows = 5; int cols = 5; int x; int** arr = new int[rows][cols] cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x]; return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){ int rows; int cols; int x;...
#include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code here } void fill(int arr[], int count){ for(int i = 0; i < count; i++){ cout << "Enter number: "; cin >> arr[i]; } } void display(int arr[], int count){ for(int i = 0; i < count; i++){ cout << arr[i] << endl; } } int main() { cout << "How many items: "; int count; cin >> count; int * arr = new...