0

Can someone help me with code.

Like now I want a user to input his own marks, my code should be able to sum up those marks and devide them with their total and get the output for his percentage, the first line of input should be an integer N stating how many times the user will enter a mark https://code.sololearn.com/c952WABm6pNP/?ref=app

26th May 2022, 11:04 PM
Siyabonga Mbendane
Siyabonga Mbendane - avatar
7 Answers
+ 2
Siyabonga Brian You could use a for loop or a while loop to iterate through the inputs, adding those inputs to a running total. The first number would set the number of courses and also the number of iterations of the loop. Like this #include <iostream> using namespace std; int main() { int nc; // number of courses int mark; // mark from course int total = 0; float percentage; cin >> nc; cout<<"enter marks of "<< nc <<" subjects"<<endl; for(int i=0; i<nc; i++){ cin >> mark; total += mark; } percentage = total/nc; cout<<"percentage obtained: "<< percentage<<'%'<<endl; return 0; } https://code.sololearn.com/ckf587h2C76i/?ref=app
27th May 2022, 12:17 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Siyabonga Brian As you are creating the input and adding it to your total, you can also assign the value of that input to a list index. IE: create an empty list and put the value in there in the order of appearance. When you have all your values, you can iterate through the list and apply total / list[i] to get the percentage of each item of the total. Keep going with your cpp course, it will show you how
27th May 2022, 1:30 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Siyabonga Brian Just change the equation in the iteration to suit the required output.
27th May 2022, 1:33 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Rik Wittkopp okay thanks man
27th May 2022, 1:41 AM
Siyabonga Mbendane
Siyabonga Mbendane - avatar
0
Rik Wittkopp thanks fam Here's another case what if I want to determine the percentage for each single mark of the inputs by user
27th May 2022, 1:18 AM
Siyabonga Mbendane
Siyabonga Mbendane - avatar
0
Rik Wittkopp for example User input : 2 1 3 Output: 0.25 0.75
27th May 2022, 1:20 AM
Siyabonga Mbendane
Siyabonga Mbendane - avatar
0
Rik Wittkopp actually I made a mistake on the description, i need a percentage for each mark entered let's take the user inputs 20 and 30; my code should sum this up and get 50 and devide each input by 50 so output will be 20/50 = 0.4 and 30/50 = 0.6
27th May 2022, 1:28 AM
Siyabonga Mbendane
Siyabonga Mbendane - avatar