Passing Values Within An Array

This is a discussion on Passing Values Within An Array within the C Programming forums, part of the General Programming Boards category; Can Anyone Help Me Figure Out What I'm Doing Wrong. If you cost post the correction or and example I ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    23

    Unhappy Passing Values Within An Array

    Can Anyone Help Me Figure Out What I'm Doing Wrong. If you cost post the correction or and example I would appreciate it greatly. I have posted all the criteria to the assignment below.
    Assignment
    Write a program which prompts the user to enter the number of values to process
    (a maximum of 100). Next prompt, and allow the user to enter each of the
    values. Store these values in an array. Print the largest value, the smallest
    value, and the average (to 2 decimal places) of the values in the array. You must
    use functions for each of these tasks. The GetValues() function is the only
    function that may change the array.

    The prototypes for the functions must be:

    void GetValues(int x[], int *pN);
    int FindBig(int x[], int n);
    int FindSml(int x[], int n);
    float FindAvg(int x[], int n);

    GetValues should put values in both the x[] array and at *pN
    FindBig should return the largest integer in the first n elements of x[]
    FindSml should return the smallest integer in the first n elements of x[]
    FindAvg should return the average of the first n elements of x[]

    A sample run is shown below (user input underlined). Note that user is prompted
    for value 1, 2, 3, ... as opposed to 0, 1, 2, ...
    .
    Enter number of integer values: 5
    Enter value 1: 20
    Enter value 2: -15
    Enter value 3: 90
    Enter value 4: 2
    Enter value 5: 30
    The largest value is: 90
    The smallest value is: -15
    The average value is: 25.40

    Code:

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    
    void GetValues(int x[i], int *pN);
    int FindBig(int x[], int n);
    int FindSml(int x[], int n);
    float FindAvg(int x[], int n);
    
    int *pN;
    int size;
    int sum;
    
    void main()
    {
    
    	int a[100], myBig, mySml, n;
    	float myAvg;
    	GetValues(a,&n);
    	myBig=FindBig(a,n);
    	mySml=FindSml(a,n);
    	myAvg=FindAvg(a,n);
    	printf("The largest value is: %d\n",myBig);
    	printf("The smallest value is: %d\n",mySml);
    	printf("The average value is: %.2f\n",myAvg);
    
    	void GetValues(int x[i], int *pN);
    	{
    		int *pN;
    		int size;
    		int sum;
    		int i;
    		// Get user input for number of values here.
    		printf("Enter Number Of Values: ");
    		scanf("%d", size);
    
    		// insure it is less than 100 
    		for(size=1; size<=100; size++)
    		{
    			printf("Enter Value: ");
    			scanf("d",size);
    			{
    				mySml = x[0];
    				myBig = x[0];
    				if(x[i]>myBig);
    				{
    					myBig = x[i];
    					return myBig;
    				}
    				if(x[i]<mySml);
    				{
    					mySml = x[i];
    					return mySml;
    				}
    			}
    			sum = 0;
    			{
    				sum+=x[];
    				myAvg=(float)((float)sum/size);
    				return myAvg;
    			}
    		}
    	}
    
    }
    Errors:

    Error 1 error C2065: 'x' : undeclared identifier 38 1 bigsml

    Error 2 error C2059: syntax error : ']'

    Error 3 error C2143: syntax error : missing ';' before '{' l

    Error 4 error C2065: 'x' : undeclared identifier

    Error 5 error C2059: syntax error : ']'

    Error 6 error C2562: 'main' : 'void' function returning a value

    Error 7 error C2065: 'x' : undeclared identifier

    Error 8 error C2059: syntax error : ']'

    Error 9 error C2065: 'x' : undeclared identifier

    Error 10 error C2059: syntax error : ']'

    Error 11 error C2562: 'main' : 'void' function returning a value

    Error 12 error C2065: 'x' : undeclared identifier

    Error 13 error C2059: syntax error : ']'

    Error 14 error C2562: 'main' : 'void' function returning a value


    Error 15 error C2059: syntax error : '}'

    Error 16 error C2143: syntax error : missing ';' before '}'

    Error 17 error C2059: syntax error : '}'

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    You are trying to define a function within a function. That is not allowed. You need to move your function definition outside of any functions.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    23
    I fixed it but Know im getting a whole new set of errors
    New Code
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    
    void GetValues(int x[], int *pN);
    int FindBig(int x[], int n);
    int FindSml(int x[], int n);
    float FindAvg(int x[], int n);
    
    int *pN;
    int size;
    int sum;
    
    void main()
    {
    
    	int a[100], myBig, mySml, n;
    	float myAvg;
    	GetValues(a,&n);
    	myBig=FindBig(a,n);
    	mySml=FindSml(a,n);
    	myAvg=FindAvg(a,n);
    	printf("The largest value is: %d\n",myBig);
    	printf("The smallest value is: %d\n",mySml);
    	printf("The average value is: %.2f\n",myAvg);
    }
    void GetValues(int x[], int *pN)
    {
    	float sum;
    	int i;
    
    	*pN = size;
    	// Get user input for number of values here.
    	printf("Enter Number Of Values: ");
    	scanf("%d", size);
    
    	// insure it is less than 100 
    	for(size=1; size<=100; size++)
    	{
    		printf("Enter Value: ");
    		scanf("d",*pN);
    		{
    			myBig =x[0];
    			mySml = x[0];
    			if(x[i]>myBig);
    			{
    				myBig = x[i];
    				return myBig;
    			}
    			if(x[i]<mySml);
    			{
    				mySml = x[i];
    				return mySml;
    			}
    		}
    		sum = 0;
    		{
    			sum+=x[i];
    			myAvg=(float)((float)sum/size);
    			return myAvg;
    		}
    	}
    }
    Error 1 error C2065: 'myBig' : undeclared identifier 42 1

    Error 2 error C2065: 'mySml' : undeclared identifier 43 1

    Error 3 error C2065: 'myBig' : undeclared identifier 44 1

    Warning 4 warning C4390: ';' : empty controlled statement found; is this the intent? 45 1

    Error 5 error C2065: 'myBig' : undeclared identifier 46 1

    Error 6 error C2065: 'myBig' : undeclared identifier 47 1

    Error 7 error C2065: 'mySml' : undeclared identifier 49 1

    Warning 8 warning C4390: ';' : empty controlled statement found; is this the intent? 50 1

    Error 9 error C2065: 'mySml' : undeclared identifier 51 1

    Error 10 error C2065: 'mySml' : undeclared identifier 52 1

    Error 11 error C2065: 'myAvg' : undeclared identifier 58 1

    Error 12 error C2065: 'myAvg' : undeclared identifier 59 1

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    Code:
    	scanf("%d", size);
    
    	// insure it is less than 100 
    	for(size=1; size<=100; size++)
    	{
    		printf("Enter Value: ");
    		scanf("d",*pN);
    		{
    			myBig =x[0];
    			mySml = x[0];
    Where have you declared your variables myBig, and mySml?

    Also look at the two scanfs.

    They are both wrong. Check this link scanf.

    Please cut and paste the entire error messages. The messages have important information that you are leaving out.

    Jim

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    Code:
    	scanf("%d", size);
    
    	// insure it is less than 100 
    	for(size=1; size<=100; size++)
    	{
    		printf("Enter Value: ");
    		scanf("d",*pN);
    		{
    			myBig =x[0];
    			mySml = x[0];
    Where have you declared your variables myBig, and mySml in GetValues()?

    Also look at the two scanfs.

    They are both wrong. Check this link scanf.

    Please cut and paste the entire error messages. The messages have important information that you are leaving out.

    Jim
    Last edited by jimblumberg; 11-06-2010 at 10:31 PM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    23
    I looked at the link and i'm not sure if I fixed it right. I put i into the scanf which should tell the PC to scan the Values.

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    
    void GetValues(int x[], int *pN);
    int FindBig(int x[], int n);
    int FindSml(int x[], int n);
    float FindAvg(int x[], int n);
    
    int *pN;
    int size;
    int sum;
    
    void main()
    {
    
    	int a[100], myBig, mySml, n;
    	float myAvg;
    	GetValues(a,&n);
    	myBig=FindBig(a,n);
    	mySml=FindSml(a,n);
    	myAvg=FindAvg(a,n);
    	printf("The largest value is: %d\n",myBig);
    	printf("The smallest value is: %d\n",mySml);
    	printf("The average value is: %.2f\n",myAvg);
    }
    void GetValues(int x[], int *pN)
    {
    	float myAvg, sum;
    	int a, i, myBig, mySml;
    
    	// Get user input for number of values here.
    	printf("Enter Number Of Values: ");
    	scanf("%d",&a);
    
    	// insure it is less than 100 
    	for(a=1; a<=100;a++)
    	{
    		printf("Enter Value: ");
    		scanf("d",&i);
    		{
    			myBig =x[0];
    			mySml = x[0];
    			if(x[i]>myBig);
    			{
    				myBig = x[i];
    				return myBig;
    			}
    			if(x[i]<mySml);
    			{
    				mySml = x[i];
    				return mySml;
    			}
    		}
    		sum = 0;
    		{
    			sum+=x[i];
    			myAvg=(float)((float)sum/size);
    			return myAvg;
    		}
    	}
    }
    I got my Errors Down to 5, I posted the full text.

    Warning 1 warning C4390: ';' : empty controlled statement found; is this the intent? c:\users\justin\documents\visual studio 2010\projects\bigsml\bigsml\bigsml.cpp 44 1 bigsml

    Error 2 error C2562: 'GetValues' : 'void' function returning a value c:\users\justin\documents\visual studio 2010\projects\bigsml\bigsml\bigsml.cpp 46 1 bigsml

    Warning 3 warning C4390: ';' : empty controlled statement found; is this the intent? c:\users\justin\documents\visual studio 2010\projects\bigsml\bigsml\bigsml.cpp 49 1 bigsml

    Error 4 error C2562: 'GetValues' : 'void' function returning a value c:\users\justin\documents\visual studio 2010\projects\bigsml\bigsml\bigsml.cpp 51 1 bigsml

    Error 5 error C2562: 'GetValues' : 'void' function returning a value c:\users\justin\documents\visual studio 2010\projects\bigsml\bigsml\bigsml.cpp 58 1 bigsml

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    23
    I fixed Something Now I'm Only Getting 1 Error

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    
    void GetValues(int x[], int *pN);
    int FindBig(int x[], int n);
    int FindSml(int x[], int n);
    float FindAvg(int x[], int n);
    
    int *pN;
    int size;
    int sum;
    
    void main()
    {
    
    	int a[100], myBig, mySml, n;
    	float myAvg;
    	GetValues(a,&n);
    	myBig=FindBig(a,n);
    	mySml=FindSml(a,n);
    	myAvg=FindAvg(a,n);
    	printf("The largest value is: %d\n",myBig);
    	printf("The smallest value is: %d\n",mySml);
    	printf("The average value is: %.2f\n",myAvg);
    }
    void GetValues(int x[], int *pN);
    int FindBig(int x[], int n);
    int FindSml(int x[], int n);
    float FindAvg(int x[], int n);
    {
    	float myAvg, sum;
    	int a, i, myBig, mySml;
    
    	// Get user input for number of values here.
    	printf("Enter Number Of Values: ");
    	scanf("%d",&a);
    
    	// insure it is less than 100 
    	for(a=1; a<=100;a++)
    	{
    		printf("Enter Value: ");
    		scanf("d",&i);
    		{
    			myBig =x[0];
    			mySml = x[0];
    			if(x[i]>myBig);
    			{
    				myBig = x[i];
    				return myBig;
    			}
    			if(x[i]<mySml);
    			{
    				mySml = x[i];
    				return mySml;
    			}
    		}
    		sum = 0;
    		{
    			sum+=x[i];
    			myAvg=(float)((float)sum/size);
    			return myAvg;
    		}
    	}
    }

    Error 1 error C2447: '{' : missing function header (old-style formal list?) c:\users\justin\documents\visual studio 2010\projects\bigsml\bigsml\bigsml.cpp 30 1 bigsml

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    Code:
    		scanf("d",&i);
    		{
    			myBig =x[0];
    			mySml = x[0];
    			if(x[i]>myBig);
    			{
    				myBig = x[i];
    				return myBig;
    			}
    			if(x[i]<mySml);
    			{
    				mySml = x[i];
    				return mySml;
    			}
    		}
    The first warning message "empty controlled statement found" is line 44.
    [code]
    if(x[i]>myBig); // But this is where the error is. ( semicolon should not be here)
    { // This is line 44.
    myBig = x[i];
    return myBig;
    }

    Warning 3 is the same but on line 48 .

    The rest of the errors should be self explanatory. A void function can not return a value.

    Your second scanf is still wrong

    Code:
                    scanf("d",&i);  /// missing the "%" 
    		{
    Also the function block starting after this scanf is not needed, and it might cause problems. You should probably remove it and it's matching close.

    Jim

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    23
    BTW Thank you for all your help, im new to programing and don't really understand arrays that well.

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    
    void GetValues(int x[], int *pN);
    int FindBig(int x[], int n);
    int FindSml(int x[], int n);
    float FindAvg(int x[], int n);
    
    int *pN;
    int size;
    int sum;
    
    void main()
    {
    
    	int a[100], myBig, mySml, n;
    	float myAvg;
    	GetValues(a,&n);
    	myBig=FindBig(a,n);
    	mySml=FindSml(a,n);
    	myAvg=FindAvg(a,n);
    	printf("The largest value is: %d\n",myBig);
    	printf("The smallest value is: %d\n",mySml);
    	printf("The average value is: %.2f\n",myAvg);
    }
    void GetValues(int x[], int *pN);
    int FindBig(int x[], int n);
    int FindSml(int x[], int n);
    float FindAvg(int x[], int n)
    {
    	float myAvg, sum;
    	int a, i, myBig, mySml;
    
    	// Get user input for number of values here.
    	printf("Enter Number Of Values: ");
    	scanf("%d",&a);
    
    	// insure it is less than 100 
    	for(a=1; a<=100;a++)
    	{
    		printf("Enter Value: ");
    		scanf("%d",&n);
    
    			myBig = x[0];
    			mySml = x[0];
    
    			if(x[i]>myBig)
    			{
    				myBig = x[i];
    				return myBig;
    			}
    			if(x[i]<mySml)
    			{
    				mySml = x[i];
    				return mySml;
    			}
    		sum = 0;
    		{
    			sum+=x[i];
    			myAvg=(float)((float)sum/size);
    			return myAvg;
    		}
    	}
    }
    Errors;


    Error 8 error LNK1120: 3 unresolved externals C:\Users\Justin\documents\visual studio 2010\Projects\bigsml\Debug\bigsml.exe 1 1 bigsml

    Error 6 error LNK2019: unresolved external symbol "int __cdecl FindBig(int * const,int)" (?FindBig@@YAHQAHH@Z) referenced in function _main
    C:\Users\Justin\documents\visual studio 2010\Projects\bigsml\bigsml\bigsml.obj bigsml

    Error 5 error LNK2019: unresolved external symbol "int __cdecl FindSml(int * const,int)" (?FindSml@@YAHQAHH@Z) referenced in function _main C:\Users\Justin\documents\visual studio 2010\Projects\bigsml\bigsml\bigsml.obj bigsml

    Error 7 error LNK2019: unresolved external symbol "void __cdecl GetValues(int * const,int *)" (?GetValues@@YAXQAHPAH@Z) referenced in function _main C:\Users\Justin\documents\visual studio 2010\Projects\bigsml\bigsml\bigsml.obj bigsml

    Warning 1 warning C4244: 'return' : conversion from 'int' to 'float', possible loss of data c:\users\justin\documents\visual studio 2010\projects\bigsml\bigsml\bigsml.cpp 48 1 bigsml

    Warning 2 warning C4244: 'return' : conversion from 'int' to 'float', possible loss of data c:\users\justin\documents\visual studio 2010\projects\bigsml\bigsml\bigsml.cpp 53 1 bigsml

    Warning 4 warning C4700: uninitialized local variable 'i' used c:\users\justin\documents\visual studio 2010\projects\bigsml\bigsml\bigsml.cpp 45 1 bigsml

    Warning 3 warning C4715: 'FindAvg' : not all control paths return a value c:\users\justin\documents\visual studio 2010\projects\bigsml\bigsml\bigsml.cpp 62 1 bigsml

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    The unresolved external symbols are because you are calling functions that you have not written.

    The warnings "return" conversion from int to float mean that on the line quoted you are returning an integer when you have told the compiler you will be returning a float. What type of variables are being returned at the lines quoted?

    The last 2 warnings are being caused by this block.

    Code:
    		sum = 0;
    		{
    			sum+=x[i];
    			myAvg=(float)((float)sum/size);
    			return myAvg;
    		}
    Remember the last post I told you about the extra block after the scanf. Well this is the same thing. Plus you have a return statement inside your for loop. These 3 lines should be after the loop and without the enclosing "{}".

    Jim

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    23
    I dont understand I wrote the functions here:
    Code:
    	myBig = x[0];
    		mySml = x[0];
    
    		if(x[i]>myBig)
    		{
    			myBig = x[i];
    			return myBig;
    		}
    		if(x[i]<mySml)
    		{
    			mySml = x[i];
    			return mySml;
    		}
    	}
    	sum = 0;
    	sum+=x[i];
    	myAvg=(float)((float)sum/size);
    	return myAvg;

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    What don't you understand.

    The unresolved error messages are because you have not written these functions
    Code:
    void GetValues(int x[], int *pN);
    int FindBig(int x[], int n);
    int FindSml(int x[], int n);
    To get rid of that error message you can do this:
    Code:
    void GetValues(int x[], int *pN)
    {
    }
    
    int FindBig(int x[], int n)
    {
       return 0;
    }
    
    int FindSml(int x[], int n)
    {
       return 0;
    }
    Then fill in the functions as you go along.

    Jim

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Where are the functions? All you have are prototypes listed in your code:
    Code:
    void GetValues(int x[], int *pN);
    int FindBig(int x[], int n);
    int FindSml(int x[], int n);
    If you understand what you're doing, you're not learning anything.

  14. #14
    Registered User
    Join Date
    Nov 2010
    Posts
    23
    Okay I got all the Errors away thank you, but Now it prints this code continuously. Its supposed to stop after it prints the number of values inputed

    Code:
    	for(*pN=1; *pN<=100; *pN++)
    	{
    		printf("Enter Value: ");
    		scanf("%d",&*pN);
    	}

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    Please post the complete modified code.

    Jim

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 07-28-2009, 04:15 PM
  2. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  3. Replies: 12
    Last Post: 04-12-2009, 06:49 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Inputted values into an array
    By Panther in forum C Programming
    Replies: 6
    Last Post: 04-11-2003, 11:15 AM

Tags for this Thread


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