Page 1 of 3 123 LastLast
Results 1 to 10 of 21
  1. #1

    Thumbs up c++ loop problem


    hello guys, naa unta ko ipatan-aw ninyo kung unsa sayop or kulang ani nga codes. ang problem mao ni :

    Make a program to input 10 names of student. Each student has 5 exam scores. Determine the average exam score of each student. Output the highest average exam score.

    mao ni ang code :



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

    main()
    {

    int x,y;
    char name;
    float score;
    int sum=0;
    float ave=0,max=0;

    for(x=1;x<=4;x++)
    {
    printf("Enter name %d : \n", x);
    scanf("%s", &name);
    sum = 0;

    for(y=1;y<=5;y++)
    {
    printf("Enter score %d :", y);
    scanf("%f", &score);
    sum=sum+score;
    }
    ave=(sum/5);
    printf("The average score is %.2f \n", ave);
    }


    if(ave>max)

    max=ave;

    printf("The highest average is %.2f", max);

    getch();
    }

    ang problem man gud kay kung ako butangan apilido ang name or kanang naay space kay dli na xa mangayo pa sa ikaduha nga name din dli pud xa mu compute sa sakto na average for example mu input ko ug score na :
    ( 90+90+90+60.5+80.4 ) divided by 5 kay ang mu output niya kay 82.00 which is wrong dapat 82.12 unta.

    i hope matabangan ko ninyo ani. salamat daan.

  2. #2
    1st and foremost, this is basically a C program. I think imu rang giingon na c++ ky borland C++ or some other C++ compiler imung gigamit. I could be wrong. >_<

    what i noticed is, ang imung gigamit na datatype sa name kay char.
    Maybe you should try using "char* name" instead of "char name".

    Next ang imung sum kay int ang datatype. even if float ang datatype sa imung score, inig sud niya sa sum ky ma-drop ang iyang values after the decimal point.

    For now, i guess mao lang sa ako ma-contribute. . medyo hasol man i-trace ang loops. . >_<
    Hope this helps a bit.

  3. #3
    @jc : thank you sa imu advice ako na giilisan tanan pero ang kana nalang jud nga name kato imu gi suggest nga char* name kay pareho ra gihapon sa char name, naa pa ba koy iilis ana sa may scanf dapit ?.. or idugang pa sa #include ?.. thanks.. and iya nalang jud problema kay dli xa mu loop sa next nga name instead mu diretso ra xag pangayo sa score sa next nga name or name 2..

  4. #4
    @jc : yup!, dev c++ ako gamit kanang bloodshed, nag gamit man ko sauna ug borland nga compiler. ang dev c man gud kay gamay ra ug size compare sa borland din dli pud ko kibaw mo configure ato kay ako man friend nag set up gud ato din wla napud mi magkita karon.. hehehe..

  5. #5
    hmm. . . you can try adding the #include<string.h>.
    naa ni syay function na gets() na murag scanf, only optimized for strings.

    you use it like this:

    char* name;
    gets(name);

    what this does is iyang i-treat as 1 string ang tanang characters na imung i-type until you press the enter key.
    you can use a character array if you like but mu-prefer ko ug gamit ug string pointer if dili ko sure sa string length.

  6. #6
    @jc : mao ra gihapon xa, dli ghapon xa maka loop sa next nga name instead mangayo ra xag dritso sa score sa next nga name.. huhuhu..

  7. #7
    hmm. . . is this an urgent program? i'll try to actually work on the code when I get home tonight.

  8. #8
    Quote Originally Posted by public7enemy View Post
    hello guys, naa unta ko ipatan-aw ninyo kung unsa sayop or kulang ani nga codes. ang problem mao ni :

    Make a program to input 10 names of student. Each student has 5 exam scores. Determine the average exam score of each student. Output the highest average exam score.

    mao ni ang code :



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

    main()
    {

    int x,y;
    char name;
    float score;
    int sum=0;
    float ave=0,max=0;

    for(x=1;x<=4;x++)
    {
    printf("Enter name %d : \n", x);
    scanf("%s", &name);
    sum = 0;

    for(y=1;y<=5;y++)
    {
    printf("Enter score %d :", y);
    scanf("%f", &score);
    sum=sum+score;
    }
    ave=(sum/5);
    printf("The average score is %.2f \n", ave);
    }


    if(ave>max)

    max=ave;

    printf("The highest average is %.2f", max);

    getch();
    }

    ang problem man gud kay kung ako butangan apilido ang name or kanang naay space kay dli na xa mangayo pa sa ikaduha nga name din dli pud xa mu compute sa sakto na average for example mu input ko ug score na :
    ( 90+90+90+60.5+80.4 ) divided by 5 kay ang mu output niya kay 82.00 which is wrong dapat 82.12 unta.

    i hope matabangan ko ninyo ani. salamat daan.

    Mao ni Imu sayop Bro.

    1.) Dapat you should use gets() function para ma kuha ang string bisan naay space

    2.) ang variable sa imung sum bro kay INT, dapat float pud na bro. mao ng walay floating point nga value ang answer.

  9. #9
    just a thought

    -define your string as char* name
    -or as a char array with fixed length e.g. char name[25]

    try changing this line

    printf("Enter name %d : \n", x);

    to

    printf("Enter name %d : ", x);

  10. #10
    try this code below
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    	int x,y;	
    	float score;
    	float sum = 0;
    	float ave = 0,max = 0;
    	char* name;
    
    	for(x=1;x<=4;x++)
    	{		
    		name = (char*) malloc( sizeof(char) );
    		printf("Enter name %d : ", x);
    		gets(name);		
    		sum = 0;
    
    		for(y=1;y<=5;y++)
    		{
    			printf("Enter score %d :", y);
    			scanf("%f", &score);
    			sum = sum + score;
    		}
    		fflush(stdin);
    		ave = (sum/5);
    		printf("The average score is %.2f \n", ave);
    
    		if(ave > max)
    			max=ave;
    	}
    	printf("The highest average is %.2f", max);
    
    	getch();
    
    	return 0;
    }
    fflush(stdin) will flush out all the contents in the input buffer including new line..
    you can also use getchar() instead of fflush(stdin);

  11.    Advertisement

Page 1 of 3 123 LastLast

Similar Threads

 
  1. problem for loop * c++
    By public7enemy in forum Programming
    Replies: 28
    Last Post: 02-21-2011, 01:26 PM
  2. for loop problem
    By istoryansucks in forum Programming
    Replies: 2
    Last Post: 08-08-2010, 04:15 AM
  3. Replies: 0
    Last Post: 05-23-2008, 10:12 PM
  4. Playstation 2 Problem
    By baldog in forum Gizmos & Gadgets (Old)
    Replies: 18
    Last Post: 03-02-2006, 08:28 AM
  5. m125 problem
    By eliel73173 in forum Gizmos & Gadgets (Old)
    Replies: 6
    Last Post: 04-29-2005, 01:47 PM

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