Thread: Passing Values Within An Array

  1. #16
    Registered User
    Join Date
    Nov 2010
    Posts
    23
    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);
    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)
    {
    	// Get user input for number of values here.
    	printf("Enter Number Of Values: ");
    	scanf("%d",&*pN);
    
    	// insure it is less than 100 
    	for(*pN=1; *pN=100; pN++)
    	{
    		printf("Enter Value: ");
    		scanf("%d",&*pN);
    	}
    }
    
    int FindBig(int x[], int n)
    {
    	int myBig;
    	myBig = x[0];
    	if(x[n]>myBig)
    	{
    		myBig = x[n];
    		return myBig;
    	}
    	return 0;
    }
    
    int FindSml(int x[], int n)
    {
    	int mySml;
    	mySml = x[0];
    	if(x[n]<mySml)
    	{
    		mySml = x[n];
    		return mySml;
    	}
    	return 0;
    }
    float FindAvg(int x[], int n)
    {
    	{
    		float sum,*pN, myAvg;
    		sum = 0;
    		sum+=x[n];
    		myAvg=(float)((float)sum / *pN);
    		return myAvg;
    	}
    	return 0;
    }

  2. #17
    Registered User
    Join Date
    Nov 2010
    Posts
    23
    it should only be prints the second print statement as many times as the users number of values

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What do you think this line of code is doing? Please try to explain it clearly.

    Code:
       for(*pN=1; *pN=100; pN++)
    Jim

  4. #19
    Registered User
    Join Date
    Nov 2010
    Posts
    23
    oops, Im not sure how I would word it so it only prints the statment as many times as the user inputs


    its taking the value given by the user setting it to one

    then printing the statement untill the user inputs something higher than 100
    and then adds 1 to PN everytime

  5. #20
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    But shouldn't you be doing this for the number of times the user is asking for?

    The loop should look like:

    Code:
       
       for(i = 0; i < *pN; i++)
    This way if the user says they want 10 values it will ask for 10 values.

    Also you should be filling in your array with the scanf not changing *pN.

    Code:
      scanf("%d",&x[i]);
    Jim

  6. #21
    Registered User
    Join Date
    Nov 2010
    Posts
    23
    Okay thank You Im getting a run time error, but i gtg to bed thank you for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 07-28-2009, 03:15 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. Replies: 12
    Last Post: 04-12-2009, 05: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, 10:15 AM

Tags for this Thread