Page 1 of 2 12 LastLast
Results 1 to 10 of 18
  1. #1

    Thumbs up can't print out the name of this array


    The problem is :

    Write a program that creates an array of N names each name has 5 exam scores. Output the names with the highest and lowest scores.




    Now my problem is :

    Unsa kulang nako sa kani nga code? so far mu input xag mga names din 5 ka scores din mu compute xag average din mu print out ra xag highest ug lowest na average pero dli ra mu printf ug name nga naay highest ug lowest na score.




    Here is my code :


    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    main()
    {
          char name [][30]={};
          int score [5];
          int x,y,num;
          float sum=0,ave=0;
          float large=0,min=1000;
          
          printf("Enter how many elements do you want : \n");
          scanf("%d",&num);
          
            fflush(stdin);
          for(x=0;x<num;x++)
          {
                             fflush(stdin);
                            printf("Enter name %d : \n", x);
                            gets(name[x]);
                            sum=0;
                              
                            for(y=0;y<5;y++)//inner loop
                            {
                                            printf("Enter score %d : \n", y);
                                            scanf("%d", &score[y]);
                                            
                                            if(score[y] >0)
                                            
                                            sum+=score[y];
                                            
                                            else
                                            {
                                                y=0;
                                                sum=0;
                                                }
                                                
                             }
                            
                            fflush(stdin);
                            
                            ave=(sum/5);
                            
                            printf("Average for %s is : %.2f \n", name[x], ave );
                            
                            if(ave > large)
                            large=ave;
                            
                            if(ave < min)
                             min=ave;
                            
                            
                            }//outer loop
                            
                            printf("Highest is  %s : %.2f \n", name[x], large);
                            printf("Lowest is  %s : %.2f \n", name[x], min);
                            
                            
                            
                            getch();
                            
                            }

    Unsa diay kulang ana na code? ngano dli man xa mu print ug name nga ako man xa gibutangan ug %s din name[x] ?... Hope you all can help me. Thanks!

  2. #2

    Default Re: can't print out the name of this array

    Diba gi answeran nmn ni natu nga assignment sauna?

    Anyway, logic fail mani brader. The reason dli cya mu display ug name is because of this line:
    Code:
    printf("Highest is  %s : %.2f \n", name[x], large);
    Ang value of x dri is now num + 1. So if num was given the value of 3, x here now has the value 4.

    Think. What is inside name[4]? Nga imo array is only 3 indexes long.

    Again, brader, We've told you this before. Understand lagi what you're doing. Ayaw lng pag cge ug copy paste. Wala jd kay makat-onan ana.

    Good luck.

  3. #3

    Default Re: can't print out the name of this array

    @kamahak : Nope!, ang kato before was to only print the highest average score not the name. No offense but this was not copy paste brader, bisag personalon pako nimog tan-aw dri mu program! anyway, thnx sa idea. ^^, peace!..

  4. #4

    Default Re: can't print out the name of this array

    Aw ok. I'm sorry, there was no offense intended in my earlier response. I was merely pointing out my ideals, I shouldn't have imposed myself like that.

    I'm still happy to help, brader. Always good to know that Cebu still has aspiring programmers. I hope you got it to work this time.

    Good luck.

  5. #5

    Default Re: can't print out the name of this array

    nanginahanglan paka tabang ani?ako suwayan ug check

  6. #6

    Default Re: can't print out the name of this array

    @public7enemy

    Code:
    printf("Highest is  %s : %.2f \n", name[x], large);
    printf("Lowest is  %s : %.2f \n", name[x], min);
    The value of x outside the loop is beyond the indeces of name so nothing shows.

    Bro you should get the index of the name with the largest and smallest average respectively.

    example:
    Code:
    printf("Highest is  %s : %.2f \n", name[indexOfLargest], large);
    printf("Lowest is  %s : %.2f \n", name[indexOfSmallest], min);

  7. #7

    Default Re: can't print out the name of this array

    nullpointer is correct.

    I have modified your code.
    note: please do not forget to properly comment your code.
    The modification I made here is not perfect. You may modify it whenever you feel it.

    #include<stdio.h>
    #include<conio.h>
    #include<string.h>

    main()
    {
    char name [][30]={};
    int score = 0;///int score [5]; no need to use arrays since it was not used in this code
    int x,y,num;
    int index[2]; // to indicate who is highest/lowest
    float sum=0,ave=0;
    float large=0,min=1000;

    printf("Enter how many elements do you want :");
    scanf("%d",&num);

    fflush(stdin);

    for(x=0; x<num; x++)
    {
    fflush(stdin);
    printf("\nEnter name %d : ", (x+1));
    gets(name[x]);
    sum=0;

    for(y=0;y<5;y++)//inner loop
    {
    printf("\nEnter score %d : ", (y+1));
    scanf ("%d", &score); //scanf("%d", &score[y]); --> i think this code is useless

    /*if(score[y] >0) {
    sum+=score[y];
    } */ //--> so is this
    if (score > 0) {
    sum += score;
    } else {
    y=0;
    sum=0;
    }
    }

    fflush(stdin);

    ave=(sum/5);

    printf("Average for %s is : %.2f \n", name[x], ave );

    if(ave > large) {
    large = ave;
    index[0] = x; //store index of the largest avg
    }

    if(ave < min) {
    min=ave;
    index[1] = x; //store index of the lowest avg
    }

    }//outer loop
    // show highest average score
    printf("\nHighest is %s : %.2f \n", name[index[0]], large);
    // show lowest average score
    printf("\nLowest is %s : %.2f \n", name[index[1]], min);



    getch();

    }

  8. #8

    Default Re: can't print out the name of this array

    Ahem. To quote:
    Quote Originally Posted by kamahak View Post
    Diba gi answeran nmn ni natu nga assignment sauna?

    Anyway, logic fail mani brader. The reason dli cya mu display ug name is because of this line:
    Code:
    printf("Highest is  %s : %.2f \n", name[x], large);
    Ang value of x dri is now num + 1. So if num was given the value of 3, x here now has the value 4.

    Think. What is inside name[4]? Nga imo array is only 3 indexes long.

    Good luck.
    I was actually trying to get him to figure it out himself. IMO, spoon feeding him like that would not help him, at all.

    =)

  9. #9

    Default Re: can't print out the name of this array

    you have a point bai kamahak, but I guess at one point of our life we have been spoon fed.
    Maybe right now the TS needs to be "shown" first what might be wrong in his code so that next time he will have adequate knowledge on how to "attack" this kind of problem.
    To TS, please note of the comments posted here so that you may add this to your pool of experience. =D.
    And I would suggest you create a Solutions Log (see Agile Programming), it helps you remember things that you forget...get what I mean =D. And always add good comments in your code. It helps!

  10. #10

    Default Re: can't print out the name of this array

    Just curious aning 'solutions log' nga imo gi-mention in relation to 'agile programming', bay. This might be something we have to incorporate to our existing software process. Can you provide some guides or links about the template of the 'solution log'.? Ako ng gi-search ang mga hand-outs/literature about XP programming, SCRUM, Iconix Process bisan ang Agile nga version sa MSF ug RUP pero wala pa koy nakit-an nga article nga naghisgot bahin ani. Can you shed more light on this? Interested gyud ko aning 'solution log' kay kaniadto nagmaintain man gud mi og internal wiki about all the problems we encountered and their solutions pero it became very difficult to manage kay nag-criss-cross man gud ang mga concerns (DB, code, configuration, design, etc...).

  11.    Advertisement

Page 1 of 2 12 LastLast

Similar Threads

 
  1. Help me remember what the name of this place is!
    By KendraSimone in forum Destinations
    Replies: 6
    Last Post: 06-01-2010, 02:55 PM
  2. What's the name of this Tagalog film?
    By goodasdead in forum TV's & Movies
    Replies: 4
    Last Post: 01-08-2010, 12:58 PM
  3. Replies: 8
    Last Post: 11-07-2009, 01:01 PM
  4. Please help me anyone? - Can anybody tell me the breed of this dog?
    By abewtifulmind in forum General Discussions
    Replies: 3
    Last Post: 10-26-2009, 10:11 PM
  5. what can you say bout the video of this korean love song
    By boo_joann in forum Music & Radio
    Replies: 0
    Last Post: 10-21-2009, 05:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top