some help, i get strange number.

This is a discussion on some help, i get strange number. within the C++ Programming forums, part of the General Programming Boards category; Code: float average (float x[], int n) { float sum =0.0; int i; float average; for (i=0; i<n; i++) sum ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    some help, i get strange number.

    Code:
    float average (float x[], int n)
    	{
    	float sum =0.0;
    	int  i;
    	float average;
    
    	for (i=0; i<n; i++)
    		sum +=x[i];
    		return average =(sum/float(n));
    }
    and then i get the following as output

    Code:
    Enter file name ->      nums.txt
    x[0] = 1
    x[1] = 2
    x[2] = 3
    x[3] = 76
    x[4] = 56
    x[5] = 45
    
    
    The average is: 0x00401028
    Press any key to continue
    any suggestions?
    When no one helps you out. Call google();

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,789
    How about show us the code that calls the function and prints the returned value.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Code:
    #include <fstream.h>
    #include <stdlib.h>
    // Math library
    #include <math.h>
    float average (float x[], int n)
    	{
    	float sum =0.0;
    	int  i;
    	float average;
    
    	for (i=0; i<n; i++)
    		sum +=x[i];
    		return average =(sum/float(n));
    }
    
    
    const int FileNameLen = 30, ArrayLen = 6;
    
    // The class array
    class array 
    {
      public: // public functions 
      	array(void);
    //Inline functions 
    	inline int getsize() {return(size);}
    	void read(void);
    	void write(void);
    	void     draw(void);
    private:
    
    	int x[ArrayLen];
    	int size;
    };
    
    
    array::array(void)
    
    {
    	size = 0;
    }
    
    	void array::read(void)
    {
    	size = 0;
    // Read each value 
    	do {
    	cout << "Enter a value\t?";
    	cin >> x[size++]; // Read the integers 
    } while (x[size-1] != 0 && size < ArrayLen);
    
    // Array indices from 0 to n-1 
            size = (size == ArrayLen)? size - 2: size - 1;
    }
    
    
    	void array::write(void)
    {
    	//CREATE A FILE
    
    	ofstream outfile; // file object
    	char filename[FileNameLen]; // Store the file name
    
    	// as a character string
    
    	cout << "Enter file name ->\t";
    	cin >> filename;
    
    	// Open the file
    	outfile.open(filename);
    	// If you can't open the file, print an error message
    	// and quit
    if (!outfile) {
    	cerr << "Cannot open " << filename << endl;
    	exit(1);
    }
    
    
    	for (int i = 0;  i < size;  i++) 
    	outfile << "x[" << i << "] = " << x[i] << '\n';
    
    
    }
    
    	void array::draw(void)
    {
    	ifstream infile; // file object
    	char filename[FileNameLen]; // Store the file name
    	char inchar; // as a character string
    
    	cout << "Enter file name ->\t";
    	cin >> filename;
    
    	infile.open(filename);
    
    	if (!infile) {
    	cerr << "Cannot open " << filename << endl;
    	exit(1);
    }
    
    	//Read the file 
    	while (!infile.eof())  
    {
    	inchar = infile.get(); 
    
    	cout << inchar; //Display it on screen
    
    } 
    
    infile.close();  //close the file
    
    
    }
    
    
    int main()
    { 
    
    //display file of screen
    	const int FileNameLen = 30; // constant integer
    	ifstream infile;   //file object
    
    //initialize array
    	array myarray;
    	myarray.draw();
    
    	cout << "\nThe average is: " << average << endl;
    	
    	return(0);
    }
    if u need to run the prog ull have to create a txt file that contains the array values exactly as in the output that i posted above.
    When no one helps you out. Call google();

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,789
    Code:
    cout << "\nThe average is: " << average << endl;
    You forgot to put in the parenthesis and supply the arguments to that function. Bottom line is you are not calling the function properly. Used in this manner, I suspect that the result you are seeing as output is the address of the function, which would explain the hexadecimal value.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    how do i fix it?
    When no one helps you out. Call google();

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by hk_mp5kpdw
    You forgot to put in the parenthesis and supply the arguments to that function.
    Maybe putting in the parentheses and supplying the arguments to that function.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    i still dont understand lol, on all the progs ive done so far all i had to do to get the right result was to cout the average, now this works different cuz its an array so where am i missing a parentheses and the arguments to the function?
    When no one helps you out. Call google();

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,789
    You have a class array and an object of that class called myarray. Internally, that myarray object has an array of ints that it stores and which is private to that object. You also have an outside function called average. I assume you want to display the average of the values in the int array that is contained within the myarray object. Personally, I might make the average function a member function of the class, that would simplify/elimiinate any need to pass parameters:

    Code:
    class array 
    {
    public: // public functions 
      	array(void);
    //Inline functions 
    	inline int getsize() {return(size);}
    	void read(void);
    	void write(void);
    	void draw(void);
            float average(void);
    private:
    
    	int x[ArrayLen];
    	int size;
    };
    
    float array::average()
    {
        float sum =0.0;
        int  i;
        for (i=0; i<size; i++)
            sum += x[i];
        return sum/float(size);
    }
    Then when you are ready to call the function:

    Code:
    cout << "\nThe average is: " << myarray.average() << endl;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    oooh i see, you need to use the dot operator when u calling functions that are in a class right?
    When no one helps you out. Call google();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  5. Finding a number within a number
    By jeev2005 in forum C Programming
    Replies: 2
    Last Post: 01-10-2006, 07:57 PM

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