#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    Urgent Help with C Language code.


    Any help or advice given will be of great assistance. Thank you beforehand.

    Written in C lang., the code is used to get the average test scores as the user inputs them. The tricky part is that the program is written to eliminate only the lowest score. How can I change it so it eliminates any amount of test scores the user wishes to eliminate?


    #include <stdio.h>
    #include <limits.h>
    int main()
    {
    int a = 0;
    double temp = INT_MAX, ExamValue = 0, Earray[200] = {0}, ExamValueTotal = 0, StudentGradeTotal = 0, Average = 0, Garray[200] = {0};

    printf("\n\n\t Program to determine final grade of student." );
    printf("\n\n\t Enter the Exam's Value to begin. \n\n\t Then enter the student's grade on it.\n\n\t When finished, enter in Exam's Value a -1.");

    // establish when the while loop will end. Enter a -1.
    while (ExamValue >= -0.1){

    printf ("\n\n\t Exam's Value: ");
    scanf ("%lf",&ExamValue);
    //Loop occurs along with the while. Used to indicate the proper arrays and the addition of the elements within the arrays
    if (ExamValue > -0.1){
    printf ("\n\n\t Student's grade: ");
    scanf ("%lf",&Garray[a]);
    Earray[a] = ExamValue;
    a++;
    }
    }
    if(a != 1){
    int x,y;
    //here the elements spaces reserved in memory are reduced to the proper amount. Initially there are 200 elements in memory.
    for(x = 0; x < a; x++){
    //loop that continues until the elements of x are less one element of a.
    if((Garray[x]/Earray[x]) < temp){
    temp = (Garray[x]/Earray[x]);
    y = x;
    }
    }
    while(a > -1){
    if(a != y){
    StudentGradeTotal += Garray[a];
    ExamValueTotal += Earray[a];
    }
    a--;
    }
    }else{
    while(a > -1){
    StudentGradeTotal += Garray[a];
    ExamValueTotal += Earray[a];
    a--;
    }
    }
    if (ExamValueTotal > 0){
    Average = StudentGradeTotal/ExamValueTotal * 100;
    }
    else{
    printf ("\n\n\t The Exam's Value is 0.\n\n\t Therefore there is no grade for the student.\n\n\t " );
    return 0;
    }

    printf ("\n\n\t The Average is: %lf",Average);
    printf ("\n\n\t THe final grade is: " );

    if (Average < 60)
    printf ("F" );
    else if (Average < 70)
    printf ("D" );
    else if (Average < 80)
    printf ("C" );
    else if (Average < 90)
    printf ("B" );
    else
    printf ("A" );

    printf ("\n\n\t " );
    return 0;
    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    Changes


    Highlighted in blue are the changes I have tried so far.



    #include <stdio.h>
    #include <limits.h>
    int main()
    {
    int a = 0;
    double temp = INT_MAX, ExamValue = 0, Earray[200] = {0}, ExamValueTotal = 0, StudentGradeTotal = 0, Average = 0, Garray[200] = {0};

    printf("\n\n\t Program to determine final grade of student." );
    printf("\n\n\t Enter the Exam's Value to begin. \n\n\t Then enter the student's grade on it.\n\n\t When finished, enter in Exam's Value a -1.");

    // establish when the while loop will end. Enter a -1.
    while (ExamValue >= -0.1){

    printf ("\n\n\t Exam's Value: ");
    scanf ("%lf",&ExamValue);
    //Loop occurs along with the while. Used to indicate the proper arrays and the addition of the elements within the arrays
    if (ExamValue > -0.1){
    printf ("\n\n\t Student's grade: ");
    scanf ("%lf",&Garray[a]);
    Earray[a] = ExamValue;
    a++;
    }
    }

    printf ("\n\n\t Enter the amount of tests you would like to eliminate: ");
    scanf ("%d",&VoidedExams);


    if(a != VoidedExams){
    int x,y;
    //here the elements spaces reserved in memory are reduced to the proper amount. Initially there are 200 elements in memory.
    for(x = 0; x < a-VoidedExams; x++){
    //loop that continues until the elements of x are less one element of a.
    if((Garray[x]/Earray[x]) < temp){
    temp = (Garray[x]/Earray[x]);
    y = x;
    }
    }
    while(a > -1){
    if(a != y){
    StudentGradeTotal += Garray[a];
    ExamValueTotal += Earray[a];
    }
    a--;
    }
    }else{
    while(a > -1){
    StudentGradeTotal += Garray[a];
    ExamValueTotal += Earray[a];
    a--;
    }
    }
    if (ExamValueTotal > 0){
    Average = StudentGradeTotal/ExamValueTotal * 100;
    }
    else{
    printf ("\n\n\t The Exam's Value is 0.\n\n\t Therefore there is no grade for the student.\n\n\t " );
    return 0;
    }

    printf ("\n\n\t The Average is: %lf",Average);
    printf ("\n\n\t THe final grade is: " );

    if (Average < 60)
    printf ("F" );
    else if (Average < 70)
    printf ("D" );
    else if (Average < 80)
    printf ("C" );
    else if (Average < 90)
    printf ("B" );
    else
    printf ("A" );

    printf ("\n\n\t " );
    return 0;
    }
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    How about also highlighting the rest of the code by using [code][/code] tags?

    What input did you provide? No one here is going to guess up to 200 numbers just to try and replicate your issue.

    What output did you get, and how does that differ from what you expect?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    Reply


    The user inputs each test score and value, and does so until he/she inputs a -1 on the exam value. Then the program automatically voids the lowest grade (smallest element of the Garray) and gives the average of the rest of the grades and the final grade in a percentage.

    I want to change it so I will input the total number of lowest grades that will be voided.

    Btw thank you for the advice.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    Reply


    Any value the user inputs will go through the same process. So what I truly want is to alter the process of grade elimination. The 200 elements are there to start the array, but within the code a for function and an if function are established to properly identify the number of elements the user inputs and the rest so the initial total of elements are not used up in memory.

    This is the for and if functions:

    Code:
     for(x = 0; x < a; x++){ 
    //loop that continues until the elements of x are less one element of a.
    if((Garray[x]/Earray[x]) < temp){ 
    temp = (Garray[x]/Earray[x]); 
    y = x; 
    } 
    }
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    So rather than voiding one grade, you want to void a number of them (like 5 of them)?

    This is very easy if you sort the grades, then all the lowest grades will be at one end of the array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by salem
    So rather than voiding one grade, you want to void a number of them (like 5 of them)?

    This is very easy if you sort the grades, then all the lowest grades will be at one end of the array.
    Yes that is correct. Do you know of an example of array sorting and element elimination by user input?
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    Sorting is such a common thing to do that a web search will find plenty to look at.

    C even gives you qsort() in stdlib.h.
    Perhaps seek out examples of how to use that function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    Reply


    I tried something else before reading your post last night. on the if function, where the elements of the grades and exam totals are divided, I thought it did a sort in that function (correct me if I am wrong)
    Code:
    for(x = 0; x < a; x++){ 
    //loop that continues until the elements of x are less one element of a+1.
    //loop within the for that puts together the elements of 
    the arrays by dividing them. 
    	if((Garray[x]/Earray[x]) < temp){
    		temp = (Garray[x]/Earray[x]);
    		y = x;
    	}
    }
    What I was thinking is that the VoidedExams be used as a subtraction to the if function where the grades are added
    Code:
    while(a > -1){
    //if the total elements do not equal the amount of elements with one element eliminated. 
    	if(a != y){
    	          StudentGradeTotal += Garray[a]; 
                      //StudentGradeTotal = StudentGradeTotal + 
                      Garray[a]
    	          ExamValueTotal += Earray[a];//Done until no 
                      there are no elements left to add
            }
    	a--;
    }
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    Reply


    Trying to understand how to read qsort and implementing it to the code. SO lets have this qsort right:
    Code:
    void qsort(void *base,size_t num,size_t size,int (*comp_func)(const void *, const void *))
    What do each of the segments within the function represent?
  20. #11
  21. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    RTFM and STFW.

    RTFM: Read the man page or help file on qsort to find out what the parameters do and how to use it. If you don't have documentation, then Google man page qsort.

    STFW: Google for examples. It's been around for decades, so there should be a lot of examples.

    A lot better and faster than expecting us to do a write-up from scratch.
  22. #12
  23. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Let's say the grades are 2 5 1.
    We want to eliminate the lowest two and average the rest.
    We sort the grades ascending.
    1 2 5
    We eliminate the lowest two.
    Code:
    1 2 5
       ^ start here at index 2 and work right
    There were 3 grades.
    Now there are (3-2) == 1 grade.
    We average the remaining grades.
    This involves adding together the remaining grades by inserting the addition operator, you might say (and I would say)
    giving 5
    We divide by the tally, 1.
    Giving 5.

    Do the problem by hand, will help.
    I suppose qsort isn't for babies.
    [code]Code tags[/code] are essential for python code and Makefiles!
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Programming Guides


    search
    cmanuallanguage blogspot

    for c-programs

    Comments on this post

    • clifford disagrees : Useless answer
  26. #14
  27. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Originally Posted by Hector3436
    Trying to understand how to read qsort and implementing it to the code. [...]
    What do each of the segments within the function represent?
    Some references are easier to follow that others. These:

    http://en.cppreference.com/w/cpp/algorithm/qsort
    http://www.cplusplus.com/reference/cstdlib/qsort/

    include examples that should help. The critical thing is that qsort() takes a user defined function to perform the ordering comparison, and that you have to tell it the number and size of objects to be sorted. It is usually more efficient for large objects to sort an array of pointers to the objects that to sort the objects themselves, but for small arrays it is perhaps not critical.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    Thank you


    Originally Posted by clifford
    Some references are easier to follow that others. These:

    http://en.cppreference.com/w/cpp/algorithm/qsort
    http://www.cplusplus.com/reference/cstdlib/qsort/

    include examples that should help. The critical thing is that qsort() takes a user defined function to perform the ordering comparison, and that you have to tell it the number and size of objects to be sorted. It is usually more efficient for large objects to sort an array of pointers to the objects that to sort the objects themselves, but for small arrays it is perhaps not critical.
    Thanks to all of you for your input. The program now runs as desired even without the use of qsort. Nevertheless that last reply I got is extremely useful. Thank you again.

IMN logo majestic logo threadwatch logo seochat tools logo