Thread: question: reading numbers into an array

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    34

    question: reading numbers into an array

    I'm having a heck of a time with this problem. I want this short program to read the array of numbers, and tell me how many numbers are in the array. What I'm getting besides the memory addresses, is a list of the numbers which I don't need. Just how many numbers it counts in the array. this line: printf("%d\n", outputList[numFound]); isn't required I was just using it to test to see what I'm getting. Any ideas? thank you.

    Code:
    
    #include <stdio.h>
    
    #define MAX_SIZE 100
    
    int getinput( double outputList[]); // function prototype
    
    int main (void) {
    
    
    int array[] = {3,4,5,67,5,43,2,2,3,4,5,5,6,5,4,2,3,5};
    
    getinput(array); // function call
    
    
    return 0;
    
    } // end main
    
    
    int getinput( double outputList[]) { // function definition
    
    	int numFound = 0;	// counter 
    	
    	  for (numFound = 0; numFound < ; numFound++)
                     {
    
    	     printf("%d\n", outputList[numFound]);
    	 }
    	 return numFound;
    
    } // end function getinput

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    after a quick glance what is your condition in the for loop?
    "for (numFound = 0; numFound < ; numFound++)"

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    and your function is looking for a double array, and your sending an int array

    Code:
    #include <stdio.h>
    
    #define MAX_SIZE 100    /* donno what this is for */
    
    int main (void) {
    
            int array[] = {3,4,5,67,5,43,2,2,3,4,5,5,6,5,4,2,3,5};
    
            printf("%d\n", sizeof(array)/sizeof(*array));
    
    return 0;
    
    } // end main
    this method cannot be used in another function though, since your not really sending the array, just a pointer to it.
    Last edited by sl4nted; 11-13-2006 at 10:51 PM.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    printf("%d\n", outputList[numFound]);
    You cannot print double value with the %d format
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    34
    okie I see what your saying about the int array and reading a double that explains why I kept getting that warning thank you!.

    As for what the condition the for loop is checking for I've tried everything I could think of.

    numFound < outputList[numfound]
    numFound < outputList[MAX_SIZE]
    numFound < outputList[]

    none of which works. the MAX_SIZE definition is condition on the longest possible number array that I will be presented with.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And what about value suggected by the sl4nted?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    34
    I like sl4nted's solution, however my problem is a bit more difficult since I'm using the function to actually read a list of numbers from a txt file and pass the number of elements into the main and the actual values that were read. This array will be read from a txt file I think I have the reading part figured out. I redid the code and it is doing what I want now. (printing out the elements, counting how many elements there are and passing it to main). I'm going to use fscanf to read the values in the file and pass them to main through the function, is that viable? The problem I'm going to have is I won't have a definite array size when I read the txt file, all I know is it will be 1-100 numbers, so I cannot use my line that declares how big the array is. Any Idea how I can overcome this?

    Code:
    #include <stdio.h>
    
    // Constants
    #define MAX_ARRAY_SIZE 100
     
    
    // Function Prototypes 
     int Getinput(double outPutList[]);
    
    // begin main function
    int main(void) { 
     
      // Local Variables
    
      int numfound = 0;
      double fakeArray[] = {0};
    
      // begin main work
      numfound = Getinput(fakeArray);
      
      printf("length of list: %d\n", numfound);
    
     return 0;
     
    } // end function main 
    
    
     
     int Getinput(double outPutList[]) { // function definition 
     
      // Local Variables
      double a[MAX_ARRAY_SIZE]= {4,7,6,4,23,65,2};
      double inputArraySize = 7;
      int i = 0;
     
      // Begin function work
      for (i = 0; i < inputArraySize; i++) {
        printf("%f\n", a[i]);
      } // end for
      return i;
     
    } // end function Getinput

  8. #8
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    So why not just allocate enough space for 100 numbers ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    34
    I've tried to allocate for 100 numbers, but when I execute it gives me all zero's until the last allocated space is reached so I'll have 4,7,6,4,23,65,2,0,0,0,0,0,0,... until a[99]. that wouldn't really be a problem, however, I have to use this list that is put into main, as the parameters for a few other mathematical functions, like averaging and such.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So why don't you count how many items you read in, and stop looping when you reach that number for the rest of your program?


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    34
    I'm really mystified now, I thought I had this thing working great and all I would have to do is create a while loop to read numbers from a text file, however, things are screwed up. I have my array in the function, what I want though is the inputArray to pass to fakeArray. I thought I could do that by the line outPutList[i] = inputArray[i]; and that this would pass a copy of outPutList to become fakearray. The program compiles fine, but I get a run-time error. here is what happens.

    1. program prints out elements of array inputArray
    2. program prints out how many elements are in the array
    3. program hits run-time error
    4. program prints 0 7 0 0 0 0 0 then closes

    Code:
    #include <stdio.h>
    
    // Constants
    #define MAX_ARRAY_SIZE 100
     
    
    // Function Prototypes 
     int Getinput(double outPutList[]);
    void PrintArray( double inputArray[], int inputArraySize);
    
    /////////////////////////////////////////////////////////////////////////////////////
    int main(void) { 
     
      // Local Variables
    
      int numfound = 0;
      double fakeArray[] = {0};
     
      // begin main work
    
      numfound = Getinput(fakeArray);
      
      printf("length of list: %d\n", numfound);
    
      PrintArray(fakeArray, numfound);
    
     return 0;
     
    } // end function main 
    
    
     int Getinput( double outPutList[]) { // function definition 
     
      // Local Variables
      double  const inputArray[MAX_ARRAY_SIZE] = {4,7,6,4,23,65,2};
      double inputArraySize = 7;
      int i = 0;
     
      // Begin function work
      for (i = 0; i < inputArraySize; i++) {
        printf("%f\n", inputArray[i]);
        outPutList[i] = inputArray[i];  // define what outPutList will be
      } // end for
      return i;
     
    } // end function Getinput
    
    // end file 
    
    void PrintArray(double inputArray[], int inputArraySize) { 
     
      // Local Variables
      int i = 0;
     
      // Begin
      for (i = 0; i < inputArraySize; i++) {
        printf("%d\n", inputArray[i]);
      } // end for
     
    } // end function PrintArray

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    34

    Unhappy

    Quote Originally Posted by quzah
    So why don't you count how many items you read in, and stop looping when you reach that number for the rest of your program?


    Quzah.
    I'm not sure how to do that.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    prompt user for number
    read number into variable
    
    loop while counter is less than variable
        do stuff
    Just like that.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
      double fakeArray[] = {0};
    This allocates array of 1 element.
    Use
    Code:
      double fakeArray[100];
    to allocate a little bit more.

    [edit]
    or even better
    Code:
      double fakeArray[MAX_ARRAY_SIZE];
    also
    Code:
      double  const inputArray[MAX_ARRAY_SIZE] = {4,7,6,4,23,65,2};
      double inputArraySize = 7;
    better replace with
    Code:
    double  const inputArray[] = {4,7,6,4,23,65,2};
    double inputArraySize = sizeof(inputArray)/sizeof(inputArray[0]);
    [/edit]
    Last edited by vart; 11-14-2006 at 11:46 PM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    I've never seen anyone represent the size of an array with a double before...
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Reading a list of ints from file into an array
    By mesmer in forum C Programming
    Replies: 1
    Last Post: 11-10-2008, 06:45 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Reading int's from file to array HELP plz
    By GARiMTO in forum C Programming
    Replies: 3
    Last Post: 12-14-2007, 06:12 AM
  5. array question...
    By jerrykelleyjr in forum C Programming
    Replies: 14
    Last Post: 11-14-2006, 05:50 PM