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;
}
//******************************************************
float CalcAvg(int tests[], int numTests/* must be > 0 */)
{
int sum=0;
for (int i = 0; i < numTests; i++)
{
sum = sum + tests[i];
}
avg = (float)sum/numTests;
return avg;
}
Please find the code below:::
#include <iostream>
using namespace std;
float CalcAvg(int tests[], int numTests/* must be > 0 */);
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];
}
//Add code to print the first test score. Add the
following comment://2. Print the first test score
cout<<"The first test score :
"<<tests[0]<<endl;
//Add code to print the last test score. Add the
following comment://3. Print the last test score
cout<<"The last test score :
"<<tests[5]<<endl;
//Add the code to print all of the test scores. Add a
comment: //4. Print all scores
cout<<"All scores"<<endl;
for(int i=0;i<6;i++){
cout<<tests[i]<<endl;
}
//Add code to sum the test scores. Print the sum.
Add a comment: //5. Sum all scores
for(int i=0;i<6;i++){
sum+=tests[i];
}
cout<<"Sum of all scores :
"<<sum<<endl;
//Add code to calculate and print the average test
score. Add a comment: //6. Calculate the average
float average = sum*1.0/6;
cout<<"The average of all scores :
"<<average<<endl;
//Now add code so an average calculation takes place
in a function.
//******************************************************
float average2 = CalcAvg(tests,6);
cout<<"The average calculation using function :
"<<average2<<endl;
return 0;
}
float CalcAvg(int tests[], int numTests/* must be > 0 */)
{
int sum=0;
float avg;
for (int i = 0; i < numTests; i++)
{
sum = sum + tests[i];
}
avg = (float)sum/numTests;
return avg;
}
output:
Arrays C++ #include <iostream> using namespace std; int main() { int tests[6]; // array declaration...
#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;...
// PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); // finds average of all //grades int findHighest (int [], int); // finds highest of all //grades int findFirstFail( int[]); int main() { int grades[100]; // the array holding 100 grades. int numberOfGrades; // the number of grades read. int pos; // index to the array. float avgOfGrades; // contains the average of the grades. int highestGrade; // contains the highest grade. int inderOfFail; //...
#include <iostream> using namespace std; int main() { int sumOdd = 0; // For accumulating odd numbers, init to 0 int sumEven = 0; // For accumulating even numbers, init to 0 int upperbound; // Sum from 1 to this upperbound // Prompt user for an upperbound cout << "Enter the upperbound: "; cin >> upperbound; // Use a loop to repeatedly add 1, 2, 3,..., up to upperbound int number =...
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; }
#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 <string> using namespace std; int main() { int number; int sum = 0; while(true) { cout << "Please enter a number between 1 and 11: "; cin >> number; if (number >= 1 && number <= 11) { cout << number << endl; sum = sum + number; //only add the sum when number is in range: 1-11, so add wthin this if case } else { cout << number << endl; cout << "Out of range;...
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...
C++ Redo PROG8, a previous program using functions and/or arrays. Complete as many levels as you can. Level 1: (20 points) Write FUNCTIONS for each of the following: a) Validate #students, #scores. b) Compute letter grade based on average. c) Display student letter grade. d) Display course average. Level 2: (15 points) Use ARRAYS for each of the following. e) Read a student's scores into array. f) Calculate the student's average based on scores in array. g) Display the student's...
#include<iostream> using namespace std; int main() { float num,avg,sum=0.0; int count=0; bool moreNumbers=true; cout<<"Enter grades:"<<endl; while(moreNumbers) { cin>>num; if(num < 0) { moreNumbers=false; } else { count = count+1; sum=sum+num; } } avg=sum/count; cout<<"Average grade:"<<avg<<endl; cout<<"How many grades were entered:"<<count<<endl; return 0; } Question: We need to add another loop to check input. Just like the input loop we already have, this one should use a boolean variable as a control. So, the logic could be something like this: Loop...
#include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...