Thread: Complex program using switch statement

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    5

    Complex program using switch statement

    Hi Experts , Please help me the code for below scenarion.

    Input Screen 1
    Enter the number of tests: 3
    Enter 1st Internal details as below.
    Enter Subject 1, Subject2, subject 3 Marks:
    Enter 2nd Internal details below.
    Enter Subject 1, Subject2, subject 3 Marks:
    Enter 3rd Internal details below.
    Enter Subject 1, Subject2, subject 3 Marks:

    Output Screen
    Subjects Name Total Marks Avg Marks
    S1
    S2
    S3
    Do you want to exit ? y
    If “N” Enter new students marks details
    Logic:
    For each subject the total and averages marks are calculated from the best of 3 internals which are entered.
    EX: 15 24 25
    After this need to add 25 and 24. Then take Avg and display for each subject
    25+24 = 49
    49/2 = 24.5

    To get 1st highest score among 3 internals
    IF (IA1_S1 >= IA2_S1 AND IA1_S1 >= IA3_S1) then IA1_S1 NULL (Condition fails)
    ELSE IF (IA2_S1 >= IA1_S1 AND IA2_S1 >= IA3_S1) THEN IA2_S1 NULL
    ELSE IF(IA3_S1 >= IA1_S1 AND IA3_S1 >= IA2_S1) THEN IA3_S1 25
    To get 2nd highest score among 3 internals
    IF(IA1_S1 >= IA2_S1 AND IA1_S1 >= IA3_S1)
    IF(IA2_S1> IA3_S1) THEN IA2_S1 ELSE IA3_S1 NULL
    IF(IA2_S1 >= IA1_S1 AND IA2_S1 >= IA3_S1)
    IF(IA1_S1> IA3_S1) THEN IA1_S1 ELSE IA3_S1 NULL
    IF(IA3_S1 >= IA1_S1 AND IA3_S1 >= IA2_S1)
    IF(IA2_S1> IA1_S1) THEN IA2_S1 ELSE IA1_S1 24

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    show us your code and we'll help you with it.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Sep 2015
    Posts
    5
    Hi Please find my code below. And correct me .

    Code:
    Void Main()
    {
    
    
    int test,int tes1,test2,test3;
    char s1,s2,s3;
    float total,avg;
    
    
    printf("Enter number of tests") ;
    scanf("%d",&test);
    for(test=1,test<=&test,test++)
    {
    	printf("Enter &test details below);
    	printf("Enter  Sub1 marks");
    	scanf("%d",&s1);
    	printf("Enter  Sub2 marks");
    	scanf("%d",&s2);
    	printf("Enter  Sub3 marks");
    	scanf("%d",&s3);
    	/*To get 1st highest score among 3 internals*/
    	IF (test1_s1>= test2_S1 AND test1_S1 >= test3_S1) then test1_S1 NULL (Condition fails)
    	ELSE IF (test2_S1 >= test1_S1 AND test2_S1 >= test3_S1) THEN test2_S1 NULL
    	ELSE IF(test3_S1 >= test1_S1 AND test3_S1 >= test2_S1) THEN test3_S1
    
    
    	/*To get 2nd highest score among 3 internals*/
    	IF(test1_S1 >= test2_S1 AND test1_S1 >= test3_S1)
    	IF(test2_S1> test3_S1) THEN test2_S1 ELSE test3_S1 NULL 
    	IF(test2_S1 >= test1_S1 AND test2_S1 >= test3_S1)
    	IF(test1_S1> test3_S1) THEN test1_S1 ELSE test3_S1 NULL
    	IF(test3_S1 >= test1_S1 AND test3_S1 >= test2_S1)
    	IF(test2_S1> test1_S1) THEN test2_S1 ELSE test1_S1
    }
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There are considerable problems with your code. In fact, they are so severe and so plentiful that I suggest that you take a step back and learn the basics of programming in C before proceeding this with this exercise.

    • You should #include the relevant header files.
    • Identifiers (names) in C are case-sensitive, so Void Main should be void main.
    • Actually, void main should be int main.
    • Take a close look at the for loop examples in your learning material. You would see that semi-colons are used.
    • You need to understand why &test rather than test should be used in scanf, and hence when you should use test versus &test.
    • Check the return value of scanf.
    • The if (...) then (...) that you saw in your instructions is effectively pseudocode. You need to write C code, not just copy and paste pseudocode and expect it to work in a C program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This was also asked and addressed elsewhere.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    It is better to leave that as pseudo code. You need to put the results into an array for each student, than compare the three arrays. You can find the highest mark, total marks, or average marks with a loop instead of nested if statements.

  7. #7
    Registered User
    Join Date
    Sep 2015
    Posts
    5
    Hi Thanks for all your suggestions. Could you please help the code of the above program?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rajdreams
    Could you please help the code of the above program?
    It is your program to write.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2015
    Posts
    5
    Hi I have tried. but i could not able to.. Could you please help me with the correct code?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rajdreams
    Hi I have tried. but i could not able to.. Could you please help me with the correct code?
    I gave you feedback in post #4. That is help that should allow to to come closer to correct code.

    If you are looking for me to write the complete program for you... I might do so after you have posted a working version of your program, but until you do so or come sufficiently close to doing so, nope. Such spoonfeeding does you no good and is cheating, and it does me no good should you somehow graduate thanks to all this spoonfeeding and end up working with me.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    It is actually a little bit of a hard problem if you have to make comparisons between students marks, for example, take test number 4 and there are 6 students who took that test, now make a comparison between all those students marks. The best way to do this is to use a two dimensional array, which would have rows and columns. You could pass a row or else a column as a function argument instead of having a variable number of arguments. Otherwise you might be stuck with having to have that complicated test case, like in your pseudo code. In addition, since you do not know the number of students taking the tests, that might mean that you would want to use dynamic memory allocation, otherwise you have to make sure that you have a large enough array and set limits on the number of students.

    I can see why your are going for this other route.

  12. #12
    Registered User
    Join Date
    Sep 2015
    Posts
    5
    Hi Fourangles, Thank for your reply. Could you please help me with the code ?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rajdreams
    Could you please help me with the code ?
    What help do you need? Why haven't you tried to make some progress with the suggestions that you have received, whether here or elsewhere?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    Quote Originally Posted by rajdreams View Post
    Hi Fourangles, Thank for your reply. Could you please help me with the code ?
    I would like to write the program because I would not mind to test out my theory about how to design the program, however laserlight is correct, you should read a few chapters on the c programming language before you try again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'If' Statement inside the Switch statement being ignored.
    By howardbc14 in forum C Programming
    Replies: 4
    Last Post: 04-11-2015, 11:45 AM
  2. Switch statement
    By Programmer_P in forum C++ Programming
    Replies: 9
    Last Post: 07-24-2010, 03:11 AM
  3. Using Switch statement
    By Ubha in forum C Programming
    Replies: 8
    Last Post: 12-24-2006, 12:34 AM
  4. help with program using switch statement
    By Aysha in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2002, 04:43 PM
  5. complex if statement readability
    By mlupo in forum C Programming
    Replies: 3
    Last Post: 11-19-2001, 08:54 PM