Question

What did I do wrong with this C++ code? Assume that we don't need to ask...

What did I do wrong with this C++ code?

Assume that we don't need to ask users for the number of students.

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

float *grades; // a pointer used to point to an array

int size;

int count = 0; // track the number of grade/student

float grade; // the grade of a student

float average, total; // average grade and total grades

//**************start your code here************************

cout<<"How many student grades do you want to enter:";

cin>>size;

grades = new float[size];

cout<<"Enter "<<size<<" students grades"<<endl;

while(count<size)

{

cin>>grades[count];

count++;

}

//**************end your code here************************

for (int i = 0; i < count; i++)

{

cout << "grades for student " << i + 1 << ": is " << grades[i] << endl;

}

for (int i = 0; i < count; i++)

{

total = total + grades[i];

}

average = total / count;

cout << "Average grade is $" << average << endl;

delete [] grades;

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <iomanip>

using namespace std;

int main()

{

float *grades; // a pointer used to point to an array

int size;

int count = 0; // track the number of grade/student

float grade; // the grade of a student

float average, total; // average grade and total grades
total = 0; // initialize total to 0

//**************start your code here************************

cout<<"How many student grades do you want to enter:";

cin>>size;

grades = new float[size];

cout<<"Enter "<<size<<" students grades"<<endl;

while(count<size)

{

cin>>grades[count];

count++;

}

//**************end your code here************************

for (int i = 0; i < count; i++)

{

cout << "grades for student " << i + 1 << ": is " << grades[i] << endl;

}

for (int i = 0; i < count; i++)

{

total = total + grades[i];

}

average = total / count;

cout << "Average grade is $" << average << endl;

delete [] grades;

return 0;

}

Output:

How many student grades do you want to enter:5 
Enter 5 students grades
56
66
75
82
59
grades for student 1: is 56
grades for student 2: is 66
grades for student 3: is 75
grades for student 4: is 82
grades for student 5: is 59
Average grade is $67.6

Do ask if any doubt. Please up-vote.

Add a comment
Know the answer?
Add Answer to:
What did I do wrong with this C++ code? Assume that we don't need to ask...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement and test a function that uses dynamic array to store students’ grades and calcuate the...

    Implement and test a function that uses dynamic array to store students’ grades and calcuate the average grade. // This program demonstrates the use of dynamic array // it #include <iostream> #include <iomanip> using namespace std; int main() { float *grades; // a pointer used to point to an array int size = 2; grades = new float[size];    int count = 0; // track the number of grade/student float grade; // the grade of a student float average, total;...

  • How would i be able to use If/else statements on this code for it to give...

    How would i be able to use If/else statements on this code for it to give me a letter grade at the end? #include <iostream> #include <iomanip> using namespace std; int main() { double grade1, grade2, grade3, grade4, total; cout << "please enter the first grade : "; cin >> grade1; cout << "Please enter the second grade : "; cin >> grade2; cout << "Please enter the third grade : "; cin >> grade3; cout << "Please enter the...

  • (C++) I need to alter the code below to fit the given requirements. You will need...

    (C++) I need to alter the code below to fit the given requirements. You will need to move the calculations to a function. A second function to return the Letter Grade. You will need to define a namespace to contain the CONST. Also, need to define an enum for letter grades: A, B, C, D, and F. Console Student Grade Calculation Form First Name: Larry Last Name: Hobbs Homework Average: 65 Midterm Grade: 90 Final Exam Grade: 75 Student: Larry...

  • using the code above my instructor just want me to delete name from a list of...

    using the code above my instructor just want me to delete name from a list of students name. the function needs to be called delete_name. It's in c++. please no changing the code above just adding. this is pointer and dynamic array. // This is chapter 9 Pointers and Dynamic array #include< iostream> #include<string> using namespace std; //Gv //function declaration string* add_name(string*,int&); void display_names (string*,int); //main int main() //local var int size-3; string *students_names-new string[size]; //code cout<<"Enter "<<size< students names:"<<endl;...

  • Code is in C++: Im wondering how i can make the program continue to ask the...

    Code is in C++: Im wondering how i can make the program continue to ask the user to enter Y/N with the if statment. Is it possible with an If statment? #include <iostream> using namespace std; int main() { float usDollars,cYuan; float *Dollars; char choice; Dollars = &usDollars; while(usDollars >= 0){ cout <<"Enter the amount in U.S Dollars: "; cin >> usDollars; cout << usDollars<< " U.S Dollar in Chinese Yuan is :"<<*Dollars*7.09<<endl; if (choice == 'y' || choice ==...

  • Modify Assignment #18 so it drops each student’s lowest score when determining the students’ test score...

    Modify Assignment #18 so it drops each student’s lowest score when determining the students’ test score average and Letter grade. No scores less than 0 or greater than 100 should be accepted #include<iostream> #include<string> using namespace std; int main() { double average[5]; string name[5]; char grade[5]; for(int i=0; i<=5; i++) { cout<<"Enter student name: "; cin.ignore(); getline(cin,name[i]); int sum=0; for(int j=0; j<5; j++) { int grades; cout<<"Enter Grades:"<<j+1<<" : "; cin>>grades; sum+=grades; } average[i]=(float)sum/5; if(average[i]>=90 && average[i]<=100) grade[i]='A'; else if(average[i]>=80...

  • My C++ code won't loop like it should. What am I doing wrong? #include <iostream> using...

    My C++ code won't loop like it should. What am I doing wrong? #include <iostream> using namespace std; int main() {    int n;    int flag= 1;    cout << "Prime/Not Prime" << endl;    cout << "Enter the Number to check Prime:" << endl;    cin >> n;    int m = n / 2;    int i;    for (i = 2; i <= m; i++)        if (n % i == 0)           ...

  • #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the...

    #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--...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • graph binary search for size and time c++ //System Libraries #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <iomanip> #include <alg...

    graph binary search for size and time c++ //System Libraries #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <iomanip> #include <algorithm> using namespace std; //User Libraries //Global Constants, no Global Variables are allowed //Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc... //Function Prototypes //Execution Begins Here! int main(int argc, char** argv) { int n, i, arr[50], search, first, last, middle,count=0,count_in,tot; clock_t start, end; float duration; cout<<"Enter total number of elements :"; cin>>n; cout<<"Enter numbers"; for (i=0; i<n;i++) cin>>arr[i]; cout<<"Enter a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT