having a little trouble with my program

This is a discussion on having a little trouble with my program within the C Programming forums, part of the General Programming Boards category; I am a beginner and arrays/functions are giving me a bit of trouble. I a trying to find the highest ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    3

    having a little trouble with my program

    I am a beginner and arrays/functions are giving me a bit of trouble. I a trying to find the highest midterm score and highest final score and pass it back from the function and print. I am also trying to pass the results back as an array. Can anyone help?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int high(int result, int scores[10][2]); 
    
    int main() 
    { 
        int highScore[2]; 
    
    /* equivalent to two variables to hold the maximums */ 
        /* declare array - the first column contains the midterm scores, the second column contains the final scores */ 
    
        int scores[10][2] = {{78, 90}, {87, 88}, {65, 70}, {56, 100}, {74, 72}, 
            {33, 47}, {87, 88}, {73, 73}, {79, 83}, {95, 89}}; 
        high(highScore [2], scores[10][2]);
        
        printf("Highest midterm score: %d\n", highScore[0]);
        printf("Highest final score: %d\n", highScore[1]);
        system("pause"); 
        return 0; 
    } 
    /* end of main function */ 
    int high(int result[2], int scores[10][2]) 
    {
        for (int i = 0; i < 10; i++)
        {
            if (scores[i][0] > result[0])
            {
                result[0] = scores[i][0];
            }
        }
        
        for (int i = 0; i < 10; i++)
        {
            if (scores[i][1] > result[1])
            {
                result[1] = scores[i][1];
            }
        }
       return result[2];
    }

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    You need to initialize highscore to 0 otherwize, the junk that just happens to be in the values may be higher than the high scores.

    It also helps us to have an explanation of what you need help with, and why you think you need help. Just saying "HELP" doesn't give us a clue what in direction you need help.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    3

    getting back to u

    Quote Originally Posted by WaltP View Post
    You need to initialize highscore to 0 otherwize, the junk that just happens to be in the values may be higher than the high scores.

    It also helps us to have an explanation of what you need help with, and why you think you need help. Just saying "HELP" doesn't give us a clue what in direction you need help.

    I need to check to see if I a sending to and returning from the function correctly. Is the way I have my function going to return both the high midterm and final score?

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    No. You cannot return an array. But because you passed the array into the function, whatever you do to the array will be there when you return. This does not happen normally to single variables, though.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    3

    thanks

    Thank you! I wasn't sure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble with a C program
    By Dax-o-tron in forum C Programming
    Replies: 1
    Last Post: 08-16-2011, 11:20 PM
  2. My first program trouble
    By pal1ndr0me in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2006, 12:09 AM
  3. trouble with my first c program
    By mblazar in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 01:06 AM
  4. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM
  5. big trouble in little program
    By jonesy in forum C Programming
    Replies: 1
    Last Post: 10-04-2001, 02:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21