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

c# language Write a program that takes in a number from the user. Then it prints...
c# language Write a program that takes in a number from the user. Then it prints a statement telling the user if the number is even or odd. If the number is odd, it counts down from the number to 0 and prints the countdown on the screen, each number on a new line. If the number is even, it counts down from the number to 0, only even numbers. For example, if the user enters 5, the output will...
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 java program that takes three numbers from the user and print the greatest number...
write a java program that takes three numbers from the user and print the greatest number (using if-statement). sample output: input the first number:35 input the second number:28 input the third number:87 the greatest number:87
Problem 2: (10 marks) Write a program in C or C++ that takes a number series...
Problem 2: Write a program in C or C++ that takes a number series of size n (n integers) as input from the user, push all the numbers to the stack, and reverse the stack using recursion. Please note that this is not simply popping and printing the numbers, but the program should manipulate the stack to have the numbers stored in reverse order. In addition to the provided header file, the students can use the following function to print...
Part 2 Write a C++ program that prompts the user for an Account Number(a whole number)....
Part 2 Write a C++ program that prompts the user for an Account Number(a whole number). It will then prompt the user for the initial account balance (a double). The user will then enter either w, d, or z to indicate the desire to withdraw an amount from the bank, deposit an amount or end the transactions for that account((accept either uppercase or lowercase). You must use a switch construct to determine the account transaction and a do…while loop to...
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...
C programming Get a number from the user and write a program that checks to see...
C programming Get a number from the user and write a program that checks to see if the inputted number is equal to x*x+x and x must be greater than 0. For example, if the user inputs 12. The program should check to see if 12 = x*x+x. In this case it is true because 3*3+3 = 12.
Write a C++ program that finds the minimum number entered by the user .The user is...
Write a C++ program that finds the minimum number entered by the user .The user is allowed to enter negative numbers 0, or positive numbers. The program should be controlled with a loop statement. Break statements is not allowed to break from loop. After each number inputted by the user, The program will prompt user if they need to enter another number. User should enter either Y for yes or N for no. Make Sure the user enters either Y...
Write a program in C that prompts the user for a number of seconds and then...
Write a program in C that prompts the user for a number of seconds and then converts it to h:m:s format. Example: 5000 seconds should display as 1:23:20 (1 hour, 23 minutes, 20 seconds.) Test with several values between about 100 seconds and 10,000 seconds. use unint and remainders for this and keep it as simple as possible.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT