Question

Homework 1 - Simple Array Handling Write a working C++ program to ask the user for a set of grades which are to be stored in an array in increasing sequence. The user must first provide the number which represents the count of all grades to be provided. For example, if there will be 10 grades, the user is first prompted to provide the number 10. Subsequently, each grade is provided, one at a time. Allow for a maximum of 30 grades in defining the array. When all the grades have been sorted properly, print them all, along with the average. Process the array using a function which is invoked from the main body of the program. The main body asks for the count of grades and then invokes the grade sequencing function. You must write in proper C++ syntax and actually execute the program to ensure it works properly.

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

/*Whole answer is well commented so please copy whole answer in a .cpp file and execute
Solution Approach:
1. First program will ask user for number of grades, if it is more than 30 program will ask again
2. if grades are less than 30 then it program will create an array of float type, you can change it according to you
3. Then program will pass this array to grades_sequencing(grades) function and function will sort the array in increasing order
using selection sort(please learn about selection sort)
4. After that function will calculate average of grades by sum(grades)/grades.length and display and ans

In function call array is passes as reference so no need to return array from function

Lets understand selection sort:
arr={3,5,2,4}
first iteration: compare 3 with all elements and find index of minimum element and swap them now min is 2
now arr={2,5,3,4}
second iteration : compare 5 with and find index of min element that is 3, swap 3 and 5
now arr={2,3,5,4}
third iteration : compare 5 with and find index of min element that is 4, swap 4 and 5
now arr={2,3,4,5}

Finally array is sorted.

NOTE: If you have any other data type like int so please replace all float with int etc.
*/
//C++ program to find average of grades
#include<iostream>
using namespace std;
//Selection sort function to sequence grades
void selectionSort(float grades[], int n)
{
int i, j, min_ele_idx;

// One by one move boundary of unsorted sub array
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array means next to i
min_ele_idx = i;
for (j = i+1; j < n; j++)
if (grades[j] < grades[min_ele_idx])
min_ele_idx = j;

// Swap the found minimum element with the ith element
float temp=grades[i];
grades[i]=grades[min_ele_idx];
grades[min_ele_idx]=temp;
}
}
void grades_sequencing(float grades[],int n)
{
//calling selection sort function
selectionSort(grades,n);

float sum=0;
int i;
//now summing all the grades
for(i=0;i<n;i++)
{

sum =sum + grades[i];
}

//calculating average
float average=sum/n;
cout<<"The grades are: " <<endl;
for(i=0;i<n;i++)
cout<<grades[i]<<" ";
cout<<"\n";
cout<<"The Average is: "<<average;
}
int main()
{
//Declaring n size of array
int n;
int i;
//reading n until user enters n less than or equal 30
while(1)
{
cout<<"Please enter number of students: ";
cin>>n;
if(n<=30)
break;
}
//declaring grades array , please change data type of array based on your requirement like float to int or char etc.
float grades[n];

//Now asking user to enter n grades
cout<<"Enter grades of "<<n<<" students: ";
for(i=0;i<n;i++)
cin>>grades[i];

//Calling function to process grades
grades_sequencing(grades,n);
return 0;
}

//Image for input/output

HAPrograms\C langugelgrades.exe Please enter number of students: 5 Enter grades of 5 students: 4 3.5 4.8 3.8 3.6 The grades

Add a comment
Know the answer?
Add Answer to:
Homework 1 - Simple Array Handling Write a working C++ program to ask the user for...
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
  • Write a C program to do the following 1) request user to enter 10 integer into...

    Write a C program to do the following 1) request user to enter 10 integer into an array 2) sort the array in ascending order 3) display the sorted array 4) count the number of odd and even number in the array and print out the result 5) add the value of the odd number and even number and calculate the average of the odd and even number. display the result 6) write function addNumber() to add all the number...

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

  • in c++ A: Write a program which ask user to enter 10 student's name and their...

    in c++ A: Write a program which ask user to enter 10 student's name and their grades. Then find the average of all the grades. Output all the grades with student name of whom got the grade over the average.

  • use C++ to write the following program. needs to ask the user for n The user...

    use C++ to write the following program. needs to ask the user for n The user must put in those 5 values, probably using a for loop. NOTE: You can assume the number of values to be entered is 5. My attempted program below: (not correct, but just a basis) 4. Larger Than n In a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume that the array contains...

  • Using the following parallel array and array of vectors: // may be declared outside the main...

    Using the following parallel array and array of vectors: // may be declared outside the main function const int NUM_STUDENTS =3; // may only be declared within the main function string Students[NUM_STUDENTS] = {"Tom","Jane","Jo"}; vector <int> grades[NUM_STUDENTS] {{78,98,88,99,77},{62,99,94,85,93}, {73,82,88,85,78}}; Write a C++ program to run a menu-driven program with the following choices: 1) Display the grades 2) Add grade 3) Remove grade for all students for a selected assignment 4) Display Average for each student 5) Display the name of...

  • Write a program that will do the following. The main function should ask the user how...

    Write a program that will do the following. The main function should ask the user how many numbers it will read in. It will then create an array of that size. Next, it will call a function that will read in the numbers and fill the array (fillArray). Pass the array that was made in themain function as a parameter to this function. Use a loop that will read numbers from the keyboard and store them in the array. This...

  • Write a C++ program that will provide a user with a grade range if they enter...

    Write a C++ program that will provide a user with a grade range if they enter a letter grade. Your program should contain one function. Your function will accept one argument of type char and will not return anything (the return type will be void), but rather print statements to the console. Your main function will contain code to prompt the user to enter a letter grade. It will pass the letter grade entered by the user to your function....

  • This code should be written in php. Write a program that allows the user to input...

    This code should be written in php. Write a program that allows the user to input a student's last name, along with that student's grades in English, History, Math, Science, and Geography. When the user submits this information, store it in an associative array, like this: Smith=61 67 75 80 72 where the key is the student's last name, and the grades are combined into a single space-delimited string. Return the user back to the data entry screen, to allow...

  • Write a C++ program that asks user number of students in a class and their names....

    Write a C++ program that asks user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’ names and ranking. Follow the Steps Below Save the project as A4_StudentRanking_yourname....

  • You are to write a program which will ask the user for number of students (dynamic...

    You are to write a program which will ask the user for number of students (dynamic array of class Student). For each student, you will ask for first name and number of grades (dynamic array of grades as a variable in Student). **The program should give the student random grades from 0-100** Find the averages of each student and display the information. **Display name, grades, and average neatly in a function in the Student class called Display()** Sort the students...

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