Question

In: Computer Science

2. Write a c++ program that takes from the user the ​number of courses​ and constructs...

2. Write a c++ program that takes from the user the ​number of courses​ and constructs 3 ​dynamic 1D arrays​ with size courses+1. Each array represents a student. Each cell in the array represents a student’s mark in a course. In the last cell of each 1D array you should calculate the average mark of that student. Then output the average mark of all students in each course. Delete any allocated memory. Example Number of courses : 4 50 60 70 20 100 90 80 70 80 90 100 30 Output: 50 60 70 20​ 50 100 90 80 70 ​85 80 90 100 30 ​75 Avg of course 1 = 76.66 Avg of course 2 = 80 Avg of course 3 = 83.33 Avg of course 4 = 40.

Solutions

Expert Solution

#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
//variable declaration
int courses;
  
//display message
cout<<"Enter the number of courses: ";
cin>>courses;
float avgStu[3], avgCourse[courses];
  
//dynamic array declaration
int *std1 = new int[courses+1];
int *std2 = new int[courses+1];
int *std3 = new int[courses+1];
  
  
//display message
cout<<"Enter the marks of student: "<<endl;
avgStu[0] = 0;
avgStu[1] = 0;
avgStu[2] = 0;
  
//input the markes in each of the courses and calculate the average
for(int j=0; j<courses; j++)
{
cin>>std1[j];
avgStu[0] = avgStu[0] + std1[j];
avgCourse[j] = avgCourse[j] + std1[j];
  
}
  
//input the markes in each of the courses and calculate the average
for(int j=0; j<courses; j++)
{
cin>>std2[j];
avgStu[1] = avgStu[1] + std2[j];
avgCourse[j] = avgCourse[j] + std2[j];
}
  
//input the markes in each of the courses and calculate the average
for(int j=0; j<courses; j++)
{
cin>>std3[j];
avgStu[2] = avgStu[2] + std3[j];
avgCourse[j] = avgCourse[j] + std3[j];
}
  
//set the student average at the last column
std1[courses] = avgStu[0] / (float) courses;
std2[courses] = avgStu[1] / (float) courses;
std3[courses] = avgStu[2] / (float) courses;
  
//display the result
cout<<endl<<"Student 1 ";
for(int j=0; j<=courses; j++)
{
cout<<setw(5)<<std1[j]<<" ";
}
cout<<endl<<"Student 2 ";
for(int j=0; j<=courses; j++)
{
cout<<setw(5)<<std2[j]<<" ";
}
cout<<endl<<"Student 3 ";
for(int j=0; j<=courses; j++)
{
cout<<setw(5)<<std3[j]<<" ";
}
cout<<endl<<"Average: ";
for(int i=0; i<courses; i++)
cout<<setw(5)<<setprecision(2)<<avgCourse[i]/3.0<<" ";

return 0;
}

OUTPUT:



Related Solutions

how to write a cpp program that takes a number from a user, and print the...
how to write a cpp program that takes a number from a user, and print the sum of all numbers from one to that number on screen? using loop interation as basic as possible.
2. Write a program C++ that asks the user for a number (not necessary to force...
2. Write a program C++ that asks the user for a number (not necessary to force any particular requirements). Write a function with the following signature: double square(double x) that returns the square of the user's number (x * x). 3. Write a C++ program that asks the user for an integer. Write a function that returns 1 of the number is even, and 0 if the number is odd. Use this function signature: int isEven(int x). 4. Write a...
Write a C++ console program that prompts a user to enter information for the college courses...
Write a C++ console program that prompts a user to enter information for the college courses you have completed, planned, or are in progress, and outputs it to a nicely-formatted table. Name the CPP as you wish (only use characters, underscores and number in your file name. DO NOT use blank). Its output should look something like this: Course Year Units Grade ---------- ---- ----- ----- comsc-110 2015 4 A comsc-165 2016 4 ? comsc-200 2016 4 ? comsc-155h 2014...
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
Write a program that prompts the user for an even number from 2 to 100 until...
Write a program that prompts the user for an even number from 2 to 100 until the number 90 is encountered. Not including the 90, calculate the minimum value. In case you know what this means: DO NOT USE LISTS! We will look into the use of lists later. This has to be done in the python program. Here's what I have so far: inp = 0 min = 0 while inp != 90:     inp = int(input("Please enter an even...
Write a complete C++ program that prompts the user for and takes as input, numbers until...
Write a complete C++ program that prompts the user for and takes as input, numbers until the user types in a negative number. the program should add all of the numbers together. Then if the result is less than 20 the program should multiply the result by 3, otherwise subtract 2 from the result. Finally, the program should printout the result.
2) Write a C++ program that accepts a sentence as an input from the user. Do...
2) Write a C++ program that accepts a sentence as an input from the user. Do the following with the sentence. Please use C++ style string for this question. 1) Count the number of letters in the input 2) Change all lower case letters of the sentence to the corresponding upper case
Write a program in c++ that prompts the user to input a coin collection of number...
Write a program in c++ that prompts the user to input a coin collection of number of quarters, dimes, nickels and pennies. The program should then convert the coin collection into currency value as dollars. The coin values should all be whole numbers and the resulting currency value should be displayed with two decimals. An example of user interaction is as follows: Coin Convertor Enter number of quarters: 3 Enter number of dimes: 1 Enter number of nickels: 4 Enter...
Write a Java program that takes an array of 10 "Int" values from the user and...
Write a Java program that takes an array of 10 "Int" values from the user and determines if all the values are distinct or not. Return TRUE if all the values of the array are distinct and FALSE if otherwise.
Write a C++ program that reads numbers from the user until the user enters a Sentinel....
Write a C++ program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following: Output the sum of all even numbers Output the sum of all odd numbers Output the count of all even numbers Output the count of all odd numbers You must use loops and numbers to do this. You must not use any arrays or vectors for this program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT