Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: Need a little help with my IA

  1. #1
    Join Date
    Jan 2008
    Posts
    1,514
    Rep Power
    0

    Default Need a little help with my IA

    Is there anyway to severely shorten this code? It's a part of my IA.


    Code:
    int main();
    {
    int grad, grades, exam1, exam2, exam3, exam4, exam5, sum;
    
    switch(grades){
    case 1:
    sum = exam1; if(grad = 1);
    break;
    case 2:
    sum = exam1+exam2; if(grad = 2);
    break;
    case 3:
    sum = exam1+exam2+exam3; if(grad = 3);
    break;
    case 4:
    sum = exam1+exam2+exam3+exam4; if(grad = 4);
    break;
    case 5:
    sum = exam1+exam2+exam3+exam4+exam5; if(grad = 5);
    break;
    default:
    exit(0);
    }
    return 0;
    }
    Last edited by RickyRudy; Jan 12, 2010 at 08:33 PM.

  2. #2
    Join Date
    Oct 2005
    Posts
    745
    Rep Power
    0

    Default

    What is the significance of the if(grad=?); bits of code? It's nothing more than if(true); for each line.
    3.14159265358979323846264338327950288
    4197169399375105820974944592307816406
    28620899862803482534211706798 pi 101

  3. #3
    Join Date
    Jan 2008
    Posts
    1,514
    Rep Power
    0

    Default

    Um, Well thats not really the whole code.. just a snippet but.. The other part of the code requests a value from a user then stores it in grad.. a value between 1 - 5.. So.. if grad holds the value 1 then ... sum = exam1..

    But i just want to know if there's a way to make the entire code shorter..

  4. #4
    Join Date
    Oct 2005
    Posts
    745
    Rep Power
    0

    Default

    Do you mean less code or less instructions to be executed by the processor?

    What are the grad and grade variables for?
    3.14159265358979323846264338327950288
    4197169399375105820974944592307816406
    28620899862803482534211706798 pi 101

  5. #5
    Join Date
    Oct 2009
    Posts
    785
    Rep Power
    0

    Default

    yeah, those if(grad = ?) statement are pointless as far as I (most of us) can see. You are using a switch that does the same exact thing. Case 4 will only execute if(grades = 4) so having an additional if(grad = 4) is really pointless because the condition has already been satisfied.

    And looking at the code i would have made exam an array, create a function called sum_array(int examArray[], int grades) { ... } which would compute the sum of all elements of the array up to a specific grade.... but this last part is just my preference.

  6. #6
    Join Date
    Jan 2008
    Posts
    1,514
    Rep Power
    0

    Default

    Um ok what i meant was.. or well

    I was trying to find the average of values stored in an array.. But from everything i've tried including that switch it failed

  7. #7
    Join Date
    Jan 2009
    Posts
    2,404
    Rep Power
    0

    Default

    I don't see where you divided the sum by the # of values.

    If you're trying to average values in an array, you could use a for loop to cumulatively add the values, then divide by the incrementor grades.

    I could've just coded the answer, but I'm not the one doing the course ...
    Rooted OnePlus 2 64GB Ed, Android 5.1.1 OxygenOS ; on teifin' AT&T's network; Rooted ASUS Transformer TF101 w/ dock, Android 5.1 KatKiss; Laptop: ASUS X550C, 2.0GHzx2, 8GB, 512GB SSD, Kubuntu 15.10;
    Facebook page: Skeleville Technology Solutions

  8. #8
    Join Date
    Jan 2008
    Posts
    1,514
    Rep Power
    0

    Default

    Oh i figured it out I'm getting along slowly but surely..

    Funny thing.. I only needed one line..

    total+=grade[exno];

    Currently I'm trying to make a file.. The name of a person entered previously..
    So its like this

    FILE *report;

    report=fopen(filename, "a+");

    But i'm stumped on getting the .txt part for the filename.. So its just basically a file..

    And also.. If i have a variable initialized as a char.. But i'm using it to hold a string so.. Whenever i try to input a name with a space between it for example : Ricky Rudy
    The program skips the code after it..

    int AvG()
    {
    int studage, value, val, exno, exno1, total=0, grade[5], u, count=0 ;
    float aveg;
    char name[50], subject[30], Class[20], filename[50];

    printf("Please enter the name of a student.\n");
    scanf("%s", &name);
    printf("Please enter the class %s is in.\n", name);
    scanf("%s", &Class);
    printf("Please enter the age of %s.\n", name);
    scanf("%d", &studage);

    If i should enter the student name as something with a space it won't allow me to enter anything for the class.
    Is there any other way to fix that other than having an additional printf and scan f for the second name?
    Last edited by RickyRudy; Jan 20, 2010 at 06:51 PM.

  9. #9
    Join Date
    May 2003
    Posts
    229
    Rep Power
    0

    Default

    As far as getting the .txt on the file name look up the "strcat" function.

  10. #10
    Join Date
    Jan 2008
    Posts
    1,514
    Rep Power
    0

    Default

    Yep skiing i got that

    strcpy(&filename, name);
    strcat(&filename, name1);
    strcat(&filename, ".txt");

    Got the full name for the text Thanks for the help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •