+ 2

[C/C++] Write the following source code.

A program which inputs 4 different marks obtained from quizzes, then only add the best 3 out of 4 total marks and outputs the total. example: 1st quiz: 20 2nd quiz: 15 3rd quiz: 10 4th quiz 30 The result should be 30+20+15= 65

8th Feb 2018, 2:19 PM
Naqqash Khan
Naqqash Khan - avatar
8 ответов
+ 5
Post up what you already tried. More than happy to help.
8th Feb 2018, 2:26 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
Ah okay, gotcha. Post up what you tried and I can help advise you on what you're doing wrong. I can't do your homework for you or you won't get better at it and learn what you need to, which includes your problem solving / logic skills. If you haven't tried to solve it yourself yet, then give it a shot first and let us know if you run into particular errors. This is where flow charts come in handy, so you may want to utilize that for it. It'll allow you to think of the flow of the program and basically create a map for how you're going to program it so you can follow the flow step by step. When you break the program down into small pieces, you can do the logic of each piece and then put it all together. So just think about the question you asked above and treat it like a math problem in school; you'll program it in the same way. For this problem, you'll be using simple variables and print statements, which is something you learn in the first chapter or two of most programming languages.
8th Feb 2018, 2:38 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
You can do : #include <algorithm> ... int array[4]; // instead of a, b, c, d ... sort(array, array + 4); cout << sum(array + 1, array + 4) << endl; ...
10th Feb 2018, 7:58 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
Sorry, was in a meeting. I'll go back and comment the code for you so you can learn from it. https://code.sololearn.com/cy90yx43D7Qw/#cpp #include <iostream> using namespace std; int main(){ // quiz scores int scores [4] = {20, 15, 10, 30}; int lowestScore; int result = 0; // print scores cout << "First Quiz: " << scores[0] << '\n'; cout << "Second Quiz: " << scores[1] << '\n'; cout << "Third Quiz: " << scores[2] << '\n'; cout << "Fourth Quiz: " << scores[3] << '\n'; // set lowestScore to one of the array values for comparison against the others lowestScore = scores[0]; // sizeof() is used here to figure out the length of the array, this is more dynamic // than simply putting 4 here. for(int i = 0; i < (sizeof(scores)/sizeof(scores[0])); ++i) { if(scores[i] < lowestScore) { // Compare current element in scores array against current lowest // If it's lower, store it as the new lowest score. lowestScore = scores[i]; } // Add up all values in the array to get the total score result += scores[i]; } // Omit lowest score by subtracting it from the total result -= lowestScore; // print total score of 3 highest cout << "Total Score (3 highest): " << result << '\n'; // Divide it by number of scores (3) to get average score cout << "Average Score (3 highest): " << (result / 3.0) << '\n'; return 0; }
8th Feb 2018, 3:57 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
its my homework, got stuck in it...
8th Feb 2018, 2:28 PM
Naqqash Khan
Naqqash Khan - avatar
+ 1
#include <stdio.h> int main() { int a,b,c,d; printf("Marks for first quiz: "); scanf("%d",&a); printf("Marks for second quiz: "); scanf("%d",&b); printf("Marks for third quiz: "); scanf("%d",&c); printf("Marks for fourth quiz: "); scanf("%d",&d); int e=a+b+c+d; int f; if ( a<=b && a<=c && a<=d) { f=e-a;} if ( b<=a && b<=c && b<=d) { f=e-b;} if ( c<=b && c<=a && c<=d) { f=e-c;} if ( d<=b && d<=c && d<=a) { f=e-d;} printf("\nYour result is %d.",f); return 0; }
8th Feb 2018, 2:53 PM
Naqqash Khan
Naqqash Khan - avatar
+ 1
is this correct?
8th Feb 2018, 2:53 PM
Naqqash Khan
Naqqash Khan - avatar
0
So, did it work out for you?
9th Feb 2018, 2:46 PM
Fata1 Err0r
Fata1 Err0r - avatar