Question

Fix this C++ code so that the function prototypes come before main. Main needs to be...

Fix this C++ code so that the function prototypes come before main. Main needs to be declared first. Then call to the functions from inside of main.

#include<cstdlib>
using namespace std;

//prompt user for input
void getGrades(int gradeArray[],int size)
{
for(int i=0; i<size; i++)
{
cout<<"Enter the grade "<<i+1<<": ";
cin>>gradeArray[i];
}
}

// finding average of all numbers in array
float computeAverage(int numbers[],int size)
{
float sum=0;
for(int i=0; i<size; i++)
{
sum = sum + numbers[i]; //compute sum of all numbers in array
}
return(sum/size); // average is sum divided by numbers of elements
}

//print the array
void printInts(int intArray[],int size)
{
for(int i=0; i<size; i++)
{
cout<<intArray[i]<<" ";
}
}

//generate random numbers within given range
int getRandom(int begin,int end)
{
int randomNumber= begin + ( rand() % ( end - begin + 1 ) ); //using rand function for getting random number
return(randomNumber);
}
int main()
{
int grades[10];
getGrades(grades,10); //get input
cout<<"\nAverage of grades: "<<computeAverage(grades,10); //calculate average

int randomArray[20];
for(int i=0; i<20; i++)
{
randomArray[i]=getRandom(1,50); //fill randomArray
}
cout<<"\n\nContent of random array : ";
printInts(randomArray,20); //print randomArray
cout<<"\nAverage of random array: "<<computeAverage(randomArray,20);

return(0);
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <string>
#include <ctime>
#include <stdio.h>      
#include <stdlib.h> 

using namespace std;

//prompt user for input
void getGrades(int gradeArray[],int size);

// finding average of all numbers in array
float computeAverage(int numbers[],int size);

//print the array
void printInts(int intArray[],int size);

//generate random numbers within given range
int getRandom(int begin,int end);

int main()
{
int grades[10];
getGrades(grades,10); //get input
cout<<"\nAverage of grades: "<<computeAverage(grades,10); //calculate average

int randomArray[20];
for(int i=0; i<20; i++)
{
randomArray[i]=getRandom(1,50); //fill randomArray
}
cout<<"\n\nContent of random array : ";
printInts(randomArray,20); //print randomArray
cout<<"\nAverage of random array: "<<computeAverage(randomArray,20);

return(0);
}

void getGrades(int gradeArray[],int size)
{
for(int i=0; i<size; i++)
{
cout<<"Enter the grade "<<i+1<<": ";
cin>>gradeArray[i];
}
}

// finding average of all numbers in array
float computeAverage(int numbers[],int size)
{
float sum=0;
for(int i=0; i<size; i++)
{
sum = sum + numbers[i]; //compute sum of all numbers in array
}
return(sum/size); // average is sum divided by numbers of elements
}

//print the array
void printInts(int intArray[],int size)
{
for(int i=0; i<size; i++)
{
cout<<intArray[i]<<" ";
}
}

//generate random numbers within given range
int getRandom(int begin,int end)
{
int randomNumber= begin + ( rand() % ( end - begin + 1 ) ); //using rand function for getting random number
return(randomNumber);
}
Add a comment
Know the answer?
Add Answer to:
Fix this C++ code so that the function prototypes come before main. Main needs to be...
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
  • C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

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

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

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

  • // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); //...

    // 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; //...

  • How to measure the average performance of sorting algorithms with different sizes and numbers(with arrayList<Integer>) and...

    How to measure the average performance of sorting algorithms with different sizes and numbers(with arrayList<Integer>) and compare with a graph? I have these methods: //This one receives one size(n) parameter    public static ArrayList<Integer> RandomArray(int n){               ArrayList<Integer> randomArray = new ArrayList<Integer>(n);               Random rand = new Random(); //--- random number        rand.setSeed(System.currentTimeMillis());               for(int i = 0; i < n; i++ ) {            //loop for creating...

  • Rewrite the C++ code below so the the following conditions are met: You may create any...

    Rewrite the C++ code below so the the following conditions are met: You may create any number of classes. You may edit the main() function. Remove the variable string type completely from the entire program. -- Note: Entire program includes all classes, all functions, and main(). Get rid of every if and if else statement completely from the entire program. -- Note: Entire program includes all classes, all functions, and main(). Hint: Use polymorphism. #include <iostream> #include <vector> #include <algorithm>...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an...

    Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an array of zeros from the main function. It will set the array to the Fibonacci sequence. This sequence starts with 1 and 2 as the first 2 elements and each element thereafter is the sum of the previous two elements. (1, 2, 3, 5, 8, 13…). Add another function named findNum that will get the newly created array by fibo from the main function....

  • c++ /*This is the starter file for your final proficiency test This program has a function...

    c++ /*This is the starter file for your final proficiency test This program has a function that will generate a list of integers using the rand function. Your job is to fill the insertNum and oddCount functions.*/ #include <iostream> #include <ctime> using namespace std; //constants and function prototypes const int CAP = 100; int buildList(int[], int size); void printList(int[], int size); //your functions to implement /*This function finds even numbers in the list, and inserts a number before the even...

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
ADVERTISEMENT