Thread: Array Function

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    11

    Unhappy Array Function

    I've asked about this program in the past but have just can't figure out what to do.

    I can't figure out how to make an array in a function and call it back to a different procedure. In this program I want to be able to fill an array for the Lowest Grade and return it back. I also have to do the same with the average, but if I could figure that out if I could figure out how to do it with the Lowest.

    Only thing I'm even sure works is getting the 4 scores, then I don't know if I have to make a new array inside of the findLowest function or what.

    Objective is to enter 4 test scores for 3 students, which is done in getData no problem but then I need to figure out of those scores which one is the lowest in the "int findLowest" function and then somehow put those into an array and return the values in the array to display in the display procedure along with the average.

    I need some test format or something just plain lost tried a few things can't figure out something that works

    Code:
    Look at code below please
    Last edited by Stlcardinal50; 05-06-2009 at 02:10 AM.

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    Okay what can I do to return these arrays properly, at the moment it is taking the lowest of the first row and it wont change for any other.

    And the average is doing the same.

    Code:
    //Low Score
    #include<iostream>
    #include<iomanip>
    using namespace std;
    
    void getData(int score[][4], int rows)
    {
    	for(int R=0; R < rows; R++)
    	{
    		for(int C=0; C < 4; C++)
    		{
    			do
    			{
    				cout << "Enter test score " << (C+1) << " for student " << (R+1) << ": ";
    				cin >> score[R][C];
    			}while(score[R][C] < 0 || score[R][C] > 100);
    		}
    		cout << endl;
    	}
    }
    
    int findLowest(int score[][4], int rows)
    {
    	const int LOWS = 3;
    	int arr[LOWS];
    
    	for(int R=0; R < rows; R++)
    	{
    		int low = 100;
    
    		for(int C=0; C < 4; C++)
    		{
    			if (score[R][C] < low)
    				low = score[R][C];
    		}
    		arr[R] = low;
    		return *arr;
    	}
    }
    
    double calcAvg(int score[][4], int rows, int low)
    {
    	const int AVGS = 3;
    	double arr[AVGS];
    	double avg;
    
    	for(int R=0; R < rows; R++)
    	{
    		int TOT=0;
    
    		for(int C=0; C < 4; C++)
    		{
    			TOT += score[R][C];
    		}
    
    		avg = (TOT - low) / 3.0;
    		arr[R] = avg;
    		return *arr;
    	}
    }
    
    void display(int score[][4], int rows, int low, double avg)
    {
    	for(int R=0; R < rows; R++)
    	{
    		for(int C=0; C < 4; C++)
    		{
    		cout << "Score " << (C+1) << " for student " << (R+1) << ": " << score[R][C] << endl;
    		}
    
    		cout << "Dropped Grade: " << low << endl;
    		cout << "Average: " << avg << endl;
    		cout << endl;
    	}
    }
    
    int main()
    {
    	int sc[3][4], low;
    	double avg;
    
    	getData(sc, 3);
    	low = findLowest(sc, 3);
    	avg = calcAvg(sc, 3, low);
    	display(sc, 3, low, avg);
    
    	return 0;
    }

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    int findLowest(int score[][4], int rows)
    {
    	const int LOWS = 3;
    	int arr[LOWS];
    
    	for(int R=0; R < rows; R++)
    	{
    		int low = 100;
    
    		for(int C=0; C < 4; C++)
    		{
    			if (score[R][C] < low)
    				low = score[R][C];
    		}
    		arr[R] = low;
    		return *arr;
    	}
    }
    What do you think the red code is doing? Because it probably isn't doing what you THINK it's doing.

    Also, you are returning before you have checked all your data - check where your return statement is.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    Well i was hoping that
    Code:
    arr[R] = low;
    would assign the 0,1,2 addresses in he array with the lowest grade for each student, but as for the
    Code:
    return *arr;
    I was just trying to figure out a way to return something to point to every spot of the array, but as I said I don't know.
    But I do see the return should be after the following brace, but even then still doesn't work properly.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    staring at this now

    Code:
    int findLowest(int score[][4], int rows)
    {
    	const int LOWS = 3;
    	int arr[LOWS];
    
    	for(int R=0; R < rows; R++)
    	{
    		int low = score[R][0];
    
    		for(int C=0; C < 4; C++)
    		{
    			if (score[R][C] < low)
    				low = score[R][C];
    		}
    		arr[R] = low;
    	}
    	return *arr;
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, do I have it right you want to return an array of the lowest values in each row?

    Let's get one thing straight immediately: You can not return an array from a function. You have three choices:
    1. Pass in another array to be filled in by the function.
    2. Somehow modify the original array (e.g. sort the rows in falling/rising order) - this may not be the right thing for your program, but it can work sometimes.
    3. Allocate memory in the function and return the POINTER to the allocated array storage. Note that whilst you can make the compiler return the address of a local variable like the "arr" variable in your code, the memory used by the array will be "lost" when you leave the function - so the next function called will most likely overwrite your arr variable with garbage. Never return the address of a local variable or array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    How would I go about?

    "1. Pass in another array to be filled in by the function."

    If you have an example.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Roughly the same way as your getData function. Obviously, you don't need a 2D array to get the lowest single value for each row.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    So would i have to enter different parameters? I'm just having trouble wrapping my brain around it.

    The function will have to first test the row then take that and fill it in the array like I atempted above but does that mean to make this new array I have to make a new one in main and in the function? How to make this new array just is hard for me to visualize at the moment.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are already calling a function getData to fill in a 2D array. It should be fairly straightforward to translate the way that works into a function findLowest() that takes a 1D array to be filled in based on the 2D array passed in.

    Try to write some code, rather than overthinking it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    I guess I'm not sure how to pass the 2D array in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Passing my array to function
    By pooty tang in forum C Programming
    Replies: 8
    Last Post: 09-15-2004, 12:19 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. Replies: 3
    Last Post: 03-23-2002, 04:20 PM