Thread: Taking the mean of an array, and counting the 0 values of an array.

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Williham
    this is my latest attempt at print_numbers:
    Refer to my post #4. Notice that I declared the functions with parameters.

    Quote Originally Posted by Williham
    I get an error stating that fptr is the wrong type argument to increment, which really confuses me as this is exactly how the book I'm using ("The C Programming Language" by Brian W.Kernighan & Dennis M Richie) states how to do it!
    In the context of your function, fptr was not declared.

    Quote Originally Posted by Williham
    My thinking was that with fprintf, after looking at how it works, I don't even need the read_numbers function to do print the values.
    Indeed, and that is why your print_numbers implementation is wrong in its approach. How can you print what you have not read? So, what you end up doing is reading and printing, but why read from the file so many times when you only need to read once?

    Quote Originally Posted by Williham
    Also, still using EOF as I haven't looked into alternatives yet and it seems to work OK.
    The way you use fscanf is wrong. fscanf returns the number of items read, or EOF on read failure. This is how I would implement read_numbers:
    Code:
    size_t read_numbers(FILE *fp, int numbers[], size_t max_size)
    {
        size_t size = 0;
        while (size < max_size && fscanf(fp, "%d", &numbers[size]) == 1)
        {
            ++size;
        }
        return size;
    }
    Notice that when I call fscanf, I provide a pointer to numbers[size] so that fscanf can write the integer value read to numbers[size]. Then, I check that fscanf returns 1 because that means that fscanf did indeed read an integer.

    Implement print_numbers according to my proposed function declaration. If your function does not take any arguments, it is wrong. Period.

    EDIT:
    In your thread Trying to put numbers from a txt file into an aray, without a huge amount of luck!, Salem mentioned the functions rewind and fseek to you, with good intentions. However, you are wholly unprepared to use them properly. At least until you are done with this particular program, ban yourself from using rewind and fseek.
    Last edited by laserlight; 06-03-2012 at 08:54 AM.
    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

  2. #17
    Registered User
    Join Date
    Jun 2012
    Posts
    17
    Right, starting to feel a little embarrassed now...

    read_numbers is returning size nicely into the print_numbers function, but in trying to get fscanf to work, which I presumed I needed a loop for, the loop seems to refuse to function like a for loop has every other time I've used one...
    I thought when it ran that it would iterate from 0 to size, but I've put a printf in there to print i as it does and nothing appears!
    This is probably something entirely stupid and small so I apologist for that, but here's my broken code:

    Code:
    void print_numbers(FILE *fptr, int numbers[], size_t size)     
    {
         int max_size=100, i=0;
         size=read_numbers(fptr, numbers, max_size);
         printf("Values contained within TXT File:\n\n");
         for(i=0; i==size; i++)
         {
                  fprintf(fptr, "Number %d. %d\n",i, numbers); 
         } 
         printf("\n\n");             
    }

  3. #18
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "numbers" is a array "numbers[i]" is an int.

    Edit2: "i==size" likely wrong try "i<size"

    Edit: TURN ON Compiler warnings!!!!!

    Tim S.
    Last edited by stahta01; 06-04-2012 at 08:46 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #19
    Registered User
    Join Date
    Jun 2012
    Posts
    17
    That got the loop working properly, and I'm using Bloodshed DevC; i think the compiler warnings are on as I'm very used to seeing a lot of them! This compiled fine though, without any warnings.
    Basically with this fprintf I want it to print one integer from the array numbers[], per iteration. I thought I could use i to point to each value of the array; as in: fprintf(fptr, "Number %d. %d\n",i, numbers[i]);

    But that doesn't seem to do it either. Any ideas?

    Thanks,
    Will

  5. #20
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Re-write your print_numbers as using this prototype
    Code:
    void print_numbers(int numbers[], size_t size);
    Pass it a array with data in it and the size of the array.

    Get that to work.

    You then could go back to the old prototype if you wish.
    But, I consider doing less in the function to be better.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Williham
    Basically with this fprintf I want it to print one integer from the array numbers[], per iteration. I thought I could use i to point to each value of the array; as in: fprintf(fptr, "Number %d. %d\n",i, numbers[i]);

    But that doesn't seem to do it either. Any ideas?
    That should work. The problem is, you're trying to print to the file that you just read from. Rather, you should be printing to stdout or a different file. Basically, print_numbers should not call read_numbers. The main function, or some other function called by main, should call both of them.

    Quote Originally Posted by stahta01
    But, I consider doing less in the function to be better.
    You misunderstand the point of the FILE* parameter of print_numbers.
    Last edited by laserlight; 06-04-2012 at 11:00 AM.
    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. #22
    Registered User
    Join Date
    Jun 2012
    Posts
    17
    It worked!!!! XD
    I just had to change it to this: fprintf(stdout, "Number %d. %d\n", i, numbers[i]);
    Happy days....

    Right, I'm going to try re-arrange it to call the functions in order, like you said laser light.
    Thanks all! Big time!!

    Hopefully the count 0s, calculate mean and RMS functions shouldn't be as hard to do; now that I actually have an idea of what I'm doing thanks to your help, reading, lectures and somehow not giving up!! :P

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Williham
    I just had to change it to this: fprintf(stdout, "Number %d. %d\n", i, numbers[i]);
    Ah, but if you do that, you might as well use printf. The idea here is to call print_numbers by passing stdout as an argument. Or, you could open another file and call print_numbers with that file pointer as an argument.
    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

  9. #24
    Registered User
    Join Date
    Jun 2012
    Posts
    17
    So, you mean that other arguments could be entered and print numbers would output differently? Aka not with stdout?

    Also, may be getting a little ahead of myself here but... Once read_numbers has run, will the array numbers[size] be usable in other functions too? Or do I have to put in another return command?
    i tried putting in: return size, numbers; as well as return size, &numbers; but the result is that the print-numbers then outputs gibberish....

  10. #25
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you supply stdout as an output file to print_numbers(), would you expect the output to go to a file name "output.dat" or to the console? If you provided another file handle (say, the result of fopen("output.dat", "w")) would you expect the results to be printed to the console, or to "output.dat"?

    If you implement read_numbers() and print_numbers() as laserlight has suggested, the array lifetime is determined by code that calls those functions. read_numbers() will simply write data to that array. print_numbers() will be able to print values from that array. There is no need to do anything.

    As to your last comment about "putting in return size, numbers ...." it is quite clear you have not bothered to read or understand advice you have already been given in this thread. A return statement cannot be used return two items of information like that. Not ever.

    To spell it out ..... the statement
    Code:
        return size, numbers;
    will discard the value of size, convert the value of numbers (which is an address in memory) to an integer, and return that value. I repeat, the value of size is DISCARDED, not bundled up with numbers to be returned to the caller.

    That is why you get gibberish when you pass the returned value to print_numbers() ..... Most compilers will (if you turn up warning levels) will give you warnings about that statement (eg suspicious conversions from a pointer to an int) which is actually the way compilers attempt to let you know you will get gibberish.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #26
    Registered User
    Join Date
    Jun 2012
    Posts
    17
    grumpy, I have read ever line of this topic, multiple times; it is just all VERY new to me; so I'm sorry for not getting it straight away... Seriously this is like Russian to me, or at least it was 3 days ago; now it's more like German, aka the words make sense but it's all said backwards!
    I do remember you saying that you couldn't return an array with it's size earlier on, but I thought if I wrote them separately then I could return two things from the function. Obviously not.
    Thanks for making that clear though.
    I have made the functions as laser light suggested, although still haven't worked out the fscanf bit.. So now, that I know that the array will keep it's values after the function has run that is ok.
    Basically I'm trying my hardest to learn as much as I can before I post back here, so, being unaware of the fact that the array would keep it's values after read_numbers was run (without a return) I looked up what I could do, and found that you can return array pointers, which I tried above, and it failed.

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Williham
    Once read_numbers has run, will the array numbers[size] be usable in other functions too?
    Yes, because you pass a pointer to the first element of numbers, then modify the content of numbers through that pointer.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding an array of unique values within a 2D array
    By saadashfaq in forum C Programming
    Replies: 6
    Last Post: 11-14-2011, 12:02 AM
  2. Replies: 1
    Last Post: 04-08-2011, 11:22 AM
  3. Int Array - Stop taking values at [zero]
    By bunko in forum C Programming
    Replies: 3
    Last Post: 12-04-2008, 12:54 AM
  4. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 11:25 AM
  5. sizeof(array) returns differnt values for same array?
    By Diamonds in forum C++ Programming
    Replies: 6
    Last Post: 02-02-2003, 04:27 PM