Thread: Initializing Error

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

    Initializing Error

    I'm not sure whats going on im getting a run time error and im not really sure how to fix it. I have included a print screen image of the the error in the attachments.

    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)
    {
    	int i;
    
    	printf("Enter Number Of Values: ");
    	scanf("%d", &*pN);
    
    	for(i = 0; i < *pN; i++)
    	{
    		printf("Enter Value: ");
    		scanf("%d",&x[i]);
    	}
    }
    
    int FindBig(int x[], int n)
    {
    	int myBig,i;
    	myBig = x[0];
    	if(x[i]>myBig)
    	{
    		myBig = x[i];
    		return myBig;
    	}
    	return 0;
    }
    
    int FindSml(int x[], int n)
    {
    	int mySml,i;
    	mySml = x[0];
    	if(x[i]<mySml)
    	{
    		mySml = x[i];
    		return mySml;
    	}
    	return 0;
    }
    float FindAvg(int x[], int n)
    
    {
    	{
    		float sum,*pN, myAvg, i;
    		sum = 0;
    		sum+=x[n];
    		myAvg=(float)((float)sum / *pN);
    		return myAvg;
    	}
    	return 0;
    
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    	int myBig,i;
    	myBig = x[0];
    	if(x[i]>myBig)
    You're referencing x[i] but i isn't set to anything specific. In fact, you have the exact same problem in FindSml().
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    23
    how would i set I specific to something. Sorry I'm new to programing

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The error means you're using a variable before giving it a value. When the memory is allocated for a variable, the variable gets whatever garbage was already in that memory space.

    So if you do:
    Code:
    int i;
    printf("%d\n", i);
    It could print anything in the range of an int. You might expect it would print 0, but that's not the way C works. So you have to set i to some value before you try using it otherwise it's meaningless.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    23
    im still gettiing the same error

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    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)
    {
    	int i;
    
    	printf("Enter Number Of Values: ");
    	scanf("%d", &*pN);
    
    	for(i = 0; i < *pN; i++)
    	{
    		printf("Enter Value: ");
    		scanf("%d",&x[i]);
    	}
    }
    
    int FindBig(int x[], int n)
    {
    	int myBig,i;
    	printf("%d\n", x[i]);
    	myBig = x[0];
    	if(x[i]>myBig)
    	{
    		myBig = x[i];
    		return myBig;
    	}
    	return 0;
    }
    
    int FindSml(int x[], int n)
    {
    	int mySml,i;
    	printf("%d\n", x[i]);
    	mySml = x[0];
    	if(x[i]<mySml)
    	{
    		mySml = x[i];
    		return mySml;
    	}
    	return 0;
    }
    float FindAvg(int x[], int n)
    
    {
    	float sum,*pN, myAvg;
    	int i;
    	printf("%d\n", *pN);
    	printf("%d\n", x[i]);
    	{
    		sum = 0;
    		sum+=x[i];
    		myAvg=sum / *pN;
    		return myAvg;
    	}
    	return 0;
    }

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    int i;
    printf("%d\n", *pN);
    printf("%d\n", x[i]);
    What is the value of i here? Do you know?

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    23
    im thinking it should be taking the values inserted by the user then printing them into that function correct

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That is not what was asked. What is the value of "i"?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    int FindBig(int x[], int n)
    {
    	int myBig,i;
    	printf("%d\n", x[i]);        <--- sb x[n] should it not?
    	myBig = x[0];
    	if(x[i]>myBig)     <---- same here 
    	{
    		myBig = x[i];     <---- and here 
    		return myBig;
    	}
    	return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  4. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM