Thread: Hi all + need help with code

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    Question Hi all + need help with code

    Hi all this is my first time posting and I thought Id join this forum as I need some help now and again when I have problems with C. Also there seems to be alot of nice people on here .

    I am trying to create a program with user inputting studentID,name and there unit mark. Outputting Student ID,name, average mark and Highest and lowest mark.
    Any ideas on highest and lowest mark?

    Code:
    # include <stdio.h>
    int main ()
    		/* declare variables */
    {
    		int studentID, n, max_value;
    		float total_mark = 0, unit_mark = 0, average_mark, array;
    
    		/*Input ID*/
    		printf ("Enter your student ID: ");
    		scanf("%d",& studentID);
    
    		/*Input 12 marks + divide by 12 for average*/
    		for (n = 0;  n < 12 ; n++)
    	{
    		printf("Enter your unit mark: ");
    		scanf("%f",&unit_mark);
    		total_mark = total_mark + unit_mark;
    	}
    	average_mark = total_mark /12;
    
    	/*Work out highest and lowest mark*/
    	max_value = array[0];
    		for (n = 0;  n < 12 ; n++)
    	{
    		if (array[n] > max_value)
    		{
    			max_value = array[n];
    		}
    	}
    
    
    
    
    	
    	printf("The student %d got a total mark of %6.2f and an average mark of %6.2f, ", studentID, total_mark, average_mark);
    }
    I have tried highest mark but it has a pointer error which I sort of know what it is.

    Any help, thanks

  2. #2
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    Your array variable isn't an array, it is a float. Thus, this all falls into pieces. Declare array as one with 12 slots, and I think your code will work better.
    -S

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    2
    O ic,

    so instead of float something like this:-

    char array[12];


    but it still says C:\Program Files\PellesC\Projects\week5\week6a.c(37): warning #2096: Missing return value.
    ?
    or do I need something like

    int[] array = new int[12];

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    66
    Quote Originally Posted by Team GB
    but it still says C:\Program Files\PellesC\Projects\week5\week6a.c(37): warning #2096: Missing return value.
    ?
    or do I need something like

    int[] array = new int[12];
    You declare main as an int, but you dont return anything at the end of the function.

    so instead of float something like this:-

    char array[12];
    try this

    Code:
    float array[12];
    Last edited by jamie85; 11-25-2005 at 02:39 PM.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    Quote Originally Posted by Team GB
    O ic,

    so instead of float something like this:-

    char array[12];


    but it still says C:\Program Files\PellesC\Projects\week5\week6a.c(37): warning #2096: Missing return value.
    ?
    or do I need something like

    int[] array = new int[12];
    C doesn't dynamically allocate memory with new. Just as the guy above me said, main() has a return type of int, but I don't see anything being returned.
    I like to play pocket pool.

  6. #6
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    Quote Originally Posted by Team GB
    O ic,

    so instead of float something like this:-

    char array[12];


    but it still says C:\Program Files\PellesC\Projects\week5\week6a.c(37): warning #2096: Missing return value.
    ?
    or do I need something like

    int[] array = new int[12];
    Arrays in C can be declared like this: type name[size];

    So you would want a float array[12];

    the keyword new does not exist in C - your Java days are over, and yes, it is a hard transfer.

    IMHO, you should google some basics - on arrays and pointers in C, for example.

    Best regards,
    -S

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You should return 0 from main() if nothing went wrong (you can use other numbers to indicate different errors):
    Code:
    int main(void) {
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    Oh, and also, when you read the numbers, you are not storing them in the array currently - you should if you want to keep them and want to find the highest/lowest.
    -S

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    Question still a bit stuck

    Now that the arrays are sorted out I know I need to put my unit marks into it, but im not sure how.

    Ive got the loop figured out to find the max_value.

    Code:
    # include <stdio.h>
    int main ()
    
    		/* declare variables */
    {
    		int studentID, n;
    		float total_mark = 0, unit_mark = 0, average_mark, max_value = 0;
    		int array[11];
    
    
    		/*Input ID*/
    		printf ("Enter your student ID: ");
    		scanf("%d",& studentID);
    
    		/*Input 12 marks + divide by 12 for average*/
    		for (n = 0;  n < 12 ; n++)
    	{
    		printf("Enter your unit mark: ");
    		scanf("%f",&unit_mark);
    		total_mark = total_mark + unit_mark;
    	}
    	average_mark = total_mark /12;
    	
    	
    
    
    
    
    	/*Work out highest and lowest mark*/
    
    		for (n = 0;  n < 12 ; n= n + 1)
    	{
    		if (array[n] > max_value)
    		{
    			max_value = array[n];
    		}
    	}
    
    
    
    
    	
    	printf("The student %d got a total mark of %6.2f and an average mark of %6.2f ", studentID, total_mark, average_mark);
    
    	printf("The highest mark for student %d was %6.2f ", studentID, max_value);
    }

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    To store the values in the array....
    Code:
        for (n = 0;  n < 12 ; n++)
        {
            printf("Enter your unit mark: ");
            scanf("%f",&unit_mark);
    
            total_mark = total_mark + unit_mark;
            array[n] = unit_mark;
        }
    also your array size should be 12
    even though you are counting from 0 to 11 (which is correct) you are still storing 12 elements

    You are reading in floats so your array should be float

    Btw you really don't need the second loop. you can just keep track of the max in your first loop. Though then there'd be no need for the array at all.
    Last edited by spydoor; 12-02-2005 at 02:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM