trying to read input into an array

This is a discussion on trying to read input into an array within the C Programming forums, part of the General Programming Boards category; i am reading input into an array froman input file. it's reading the numbers but not the right ones. this ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    Unhappy trying to read input into an array

    i am reading input into an array froman input file. it's reading the numbers but not the right ones. this is my code for filling the array
    Code:
    void fill_arr(float farry[], int *sizearr){
         int fills = 0;
         while (fills < *size && scanf(%f, &farry[fills]) !=EOF)
              fills ++;
          } 
          *sizearr = fills;
      }

    then i call the add function tha i wrote
    Code:
    float add_arr(float adarry[], int sizearr){    
      int count;      
      float sum = adarry[0];    
      for(count = 1; count < sizearr; count++){ 
        sum += adarry[count];   
      }  
      return sum; 
    }
    and its not doing that correctly. i keep getting zero as the result

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    The first one won't even compile, so I dunno what you're seeing.

    You could try using a debugger to see how many times you actually go round the various loops.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    scanf(“&#37;f”, &farry[fills])
    Presumably you actually use straight double quotes in your code, like this: "

    Code:
    while (fills < *size && /* ... */
    Presumably you actually use sizearr.

    Code:
    while(/* ... */ scanf(“%f”, &farry[fills]) !=EOF)
    scanf() can return 0 if it failed (such as on "foo"), even though the end of the file has not necessarily been reached. It's best to use
    Code:
    scanf("%f", &farry[fills]) == 1
    In fill_arr(), your use of sizearr is rather confusing. With this parameter, you pass the size of the array into the function, and you also pass out the number of items read. Perhaps a signature like this would be better:
    Code:
    int fill_arr(float farry[], int sizearr){
    where you return the number of elements read. Just a thought.

    Your add_arr() function assumes that there is at least one element in the array, which, while it might be a valid assumption, is simple to change. Just initialize sum to zero and have the for loop start at zero (count = 0) as well.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    ok this is what i have now for the fill and the add is still the same
    Code:
    int fill_arr(float farry[], int *sizearr){
      int fills = 0;
      while(fills < MAX && scanf("%f", &farry[fills]) != EOF){
         fills++;
      }  
      *sizearr = fills;       
      if(*sizearr == MAX)
        return 0;  
      else 
        return 1;  
    }

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    How many numbers are actually getting read? Why don't you print each number as it is read inside that loop?
    Code:
      while(fills < MAX && scanf("&#37;f", &farry[fills]) != EOF){
         printf("reading farry[%d], got %f\n", fills, farry[fills]);
         fills++;
      }
    If everything's getting read properly, you could try out a debugger as Salem mentioned. Print your array before you read it, after you read it, before you sum it, etc.

    [edit] BTW, the standard in C is to return 0 upon success. And your other function was probably better -- using a parameter for two different purposes is a bit suspect, but probably better than hard-coding in the maximum elements in the array. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    ok..i did that and it is reading the numbers into the array. now i se that the problem comes in printing the array and adding the numbers in the array when the function is called.here are those pieces of code
    Code:
    #include <stdio.h>
    #define MAX 20
    
    /*prototypes*/
    void fill_arr(float farry[], int *sizearr); 
    
    int main(){
        char menuChoice;
        float result = 0;
    /*other variables*/
    float add[MAX]; 
    
    switch (menuChoice){
        case a:
        case A:
    /*fill array function*/
             fill_arr(filled, &sizearr);
    /*call the add function setting the result variable with its return variable*/
             result = add_arr(add, sizearr);
             break;
    
     return 0;
    }
    
    void fill_arr(float farry[], int *sizearr){
      int fills = 0;
      while(fills < MAX && scanf("%f", &farry[fills]) != EOF){
        printf("reading farry[%d], got %f\n", fills,farry[fills]);
        fills++;
      }  
      *sizearr = fills;              
    }
    
    
    float add_arr(float adarry[], int sizearr){
      int count;
      float sum = 0;
      for(count = 0; count < sizearr; count++){
        sum += adarry[count]; 
      }
      return sum;
    }
    this is the result:
    reading farry[0], got 4.000000
    reading farry[1], got 5.500000
    reading farry[2], got 6.200000
    reading farry[3], got 0.800000
    reading farry[4], got 0.000000
    reading farry[5], got 10.500000
    choice = A: result = 0.000
    There were 6 operands given
    0.000 0.000 0.000 0.000 0.000 -252445984687511491211839812599459872768.000

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
        case ‘a’:
        case ‘A’:
    Use ordinary ASCII quotes!

    Code:
             fill_arr(filled, &sizearr);
    /*call the add function setting the result variable with its return variable*/
             result = add_arr(add, sizearr);
    Umm, you're using two different arrays here. Why should they contain the same values?

    Unless this "add function" generates the add[] array from the filled[] array, in which case you should post its code here . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    I'm getting bored of seeing code which obviously won't compile, let alone run.
    How the hell are we supposed to tell you exactly where you're going wrong when you keep posting paraphrased rubbish you wrote from memory, rather than copied directly from your editor.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    sorry was copy/pasting from word thts why " were slanted. they are straight in code

    oh ok!!! i see. sorry beginner here. ok so now that that works. the printing part is not coming out right.
    Code:
     printf("choice = %c: result = %.3f\n",menuChoice,result); 
     /* code to print the number of operands given and list them 
     in the same order they were given on the next line */
     for(counted = 0; counted < sizearr; counted++){
          numoperands++;
        }
        printf("There were %d operands given\n", numoperands);
        for(counted =0; counted < sizearr; counted++){
          printf("%.3f  ",countarr[counted]);
        }
      }
      printf("\n");

  10. #10
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by dwks View Post
    Code:
        case ‘a’:
        case ‘A’:
    Use ordinary ASCII quotes!

    Code:
             fill_arr(filled, &sizearr);
    /*call the add function setting the result variable with its return variable*/
             result = add_arr(add, sizearr);
    Umm, you're using two different arrays here. Why should they contain the same values?

    Unless this "add function" generates the add[] array from the filled[] array, in which case you should post its code here . . .
    If it were only that what's wrong with it...

    Code:
    #include <stdio.h>
    #define MAX 20
    
    /*prototypes*/ /* right, prototypes. Where's the other one you use? */
    void fill_arr(float farry[], int *sizearr); 
    
    int main(){
        char menuChoice; /* random data is in menuChoice */
        float result = 0;
    /*other variables*/
    float add[MAX]; 
    
    /* Where does menuChoice get input from? */
    switch (menuChoice){
        case ‘a’: /* dwks told you already that's not the same that 'a' */
        case ‘A’:
    /*fill array function*/
             fill_arr(filled, &sizearr); /* where are filled and sizearr declared before use? */
    /*call the add function setting the result variable with its return variable*/
             result = add_arr(add, sizearr); /* same where's sizearr declared before use? */
             break;
    /* missing a } closing braquet for the switch */
     return 0;
    }
    
    void fill_arr(float farry[], int *sizearr){
      int fills = 0;
      while(fills < MAX && scanf("&#37;f", &farry[fills]) != EOF){
        printf("reading farry[%d], got %f\n", fills,farry[fills]);
        fills++;
      }  
      *sizearr = fills;              
    }
    
    
    float add_arr(float adarry[], int sizearr){
      int count;
      float sum = 0;
      for(count = 0; count < sizearr; count++){
        sum += adarry[count]; 
      }
      return sum;
    }

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
     for(counted = 0; counted < sizearr; counted++){
          numoperands++;
        }
    That loop does exactly the same thing as
    Code:
    numoperands = sizearr;
    As for this:
    Code:
        printf("There were &#37;d operands given\n", numoperands);
        for(counted =0; counted < sizearr; counted++){
          printf("%.3f  ",countarr[counted]);
        }
    I hate to say this, but you're using yet another array that might not have any relationship with the other two . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    duh!!! lol

    thanks...i'm so slow. ok last thing. I can't get the right value for my power function. i think it may have something to do with the powerval variable

    Code:
    float power_func(float firsval, int seconval){  
      int count;  
      float powerval =1.0; 
      if (seconval < 0){ 
        firsval = 1/firsval;
        seconval *= -seconval;   
      } 
      for(count = 0; count < seconval; count++){  
        powerval *= firsval;  
      }  
      return powerval; 
    }

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    seconval *= -seconval;
    Is this what you meant?
    Code:
    seconval = -seconval;
    Other than that, I don't really see anything wrong with your function. How are you calling it? With negative powers? In a loop?

    Does it work with this?
    Code:
    #include <stdio.h>
    
    /* power_func() goes here */
    
    int main() {
        printf("&#37;f\n", power_func(2, 8));
        return 0;
    }
    Expected output: 256

    BTW, there's a standard pow() function in math.h that does exactly what your function does, except it supports arbitrary powers (like 2**pi!) and is probably many times faster and likely more accurate.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    oh once again..one of those moments....thank you so much..i really appreciate it. yes that worked. i wish we ould use the power function but my ta won't let us.

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    sorry one other thing. This is working backwards for me. i get a -1 for ascending number (1 2 3 4 5). here is the code i have

    Code:
    float ascend_arr(float aarry[], int sizearr){
      int ascend;
      for(ascend = 1; ascend < sizearr;){
        if (aarry[ascend] < aarry[ascend + 1]){
          ascend++;
        } 
        else{
          return 1;
        }
      } 
      return -1; 
    }

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

Similar Threads

  1. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  2. Read file into array
    By neobarn in forum C Programming
    Replies: 6
    Last Post: 01-09-2009, 11:41 AM
  3. read file list into array
    By deltaxfx in forum C Programming
    Replies: 1
    Last Post: 01-07-2006, 10:41 PM
  4. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  5. function to read an int from input
    By linucksrox in forum C Programming
    Replies: 8
    Last Post: 06-08-2004, 10:15 AM

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