A loop and char problem

This is a discussion on A loop and char problem within the C++ Programming forums, part of the General Programming Boards category; >> int integerVariable[noOfArray]; First, you moved this line up in your code. As I said, it is technically illegal anyway, ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> int integerVariable[noOfArray];
    First, you moved this line up in your code. As I said, it is technically illegal anyway, but now it is worse because noOfArray has not been initialized. Just come up with a maximum number and use that as the size of the array instead of the noOfArray variable. Continue to use noOfArray later in the program as you currently are, though.

    >> There is something wrong with this line
    Yes, there is. At that point you are attempting to call a function. When calling a function, you pass variables as arguments to the function. You need to change that line to pass the array variable and the noOfArray variable to the function. To pass a variable you just put the variable name.

    Another thing to remember is variable scope. If you declare a variable inside the main function, you cannot use it inside another function. For example, you declare allNumbers at the top of main, but only use it inside averageFunc. You should declare it inside averageFunc. In general, you should declare and initialize variables just before you want to use them.

    Finally, you've got a decent amount of code there. It is much easier to work through problems one at a time, though. You should be adding a little bit of code, then compiling, then adding more code, etc. This way, you won't have to deal with so many compiler errors at once, and it will be easy to narrow down what changed caused the most recent error.

  2. #17
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    this thing
    Code:
    double averageFunc(int integerVariable[], int noOfArray)
    {
           for (int i = 0; i < noOfArray; i++)
        {
            allNumbers = allNumbers + integerVariable[i]; // initialize allNumbers, otherwise you'll get garbage
            average = allNumbers / noOfArray;
        }
        
        return average;
    }
    assuming you are looking for mean... why don't you just accumulate all the data, and divide it by noOfArray at the end of the iteration?
    Code:
    for(someStuff) {
       allNumbers = allNumbers + data;
    }
    
    return allNumbers/noOfArray;

  3. #18
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    >> I was thinking of converting it to ancii
    Converting? It IS ascii, how can you convert it to something that it already is
    "The Internet treats censorship as damage and routes around it." - John Gilmore

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

Similar Threads

  1. Can't get loop to work or validation
    By eminem3150 in forum C++ Programming
    Replies: 11
    Last Post: 01-15-2008, 06:21 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

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