Thread: Problem With Array

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    10

    Problem With Array

    I'm working on this code for an assignment:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define SIZE = 100
         double x[SIZE];
         #define SENTINEL -1
    
    
    
    int fillArray (double[],int);
    float x; 
    
    
    int main(void)
      
    {
     int n;
     int x; 
       
      n = fillArray (x, maxsize);
      print n
      print x
      
    }  
    
    
    
    
    
    
    
    
    // Function to read a collection of type float data from 
    //    the keyboard and store this data in the array x. 
    // Returns number of elements read if function completes as //    intended.
    // Otherwise: Returns -2 if SIZE <= 0; Returns -1 if file //    is empty.  Returns 0 if too many items in file (array //    is filled while data remains to be processed)
    
    int fillArray 
       (double x[],     // INOUT: array to be filled 
        int arraysize)  // IN: size of array
    {
        double item;  // Temp cell for each item read
        int count;    // Count of number of items read
    
        if (arraysize <= 0) return (-2);
    
        count = 0;    
        // Execute the "priming" or initial read
        printf("Enter the first data item: ");
        scanf ("%f", &item);
        // Repeat until no more data.
        while (item != SENTINEL)
        {
            x[count] = item; 
            count++;        
            if (count >= arraysize) return (0); // array full
    	   // Execute the "update" or next read
            printf("Enter the next data item: ");
            scanf ("%f", &item);
        }
        if (count == 0) return (-1); // file empty
        else return (count);
    } // end fillArray
    Can anyone help me and know why it's not compiling? Thanks.
    Last edited by mdthejericho; 10-22-2010 at 11:57 AM.

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    It would be helpful if you stated the specific errors your compiler gives you when you try and compile the code.

    I will give you a few clues:
    1) You define 'x' as an array of doubles and then redefine it as a single float

    2) You haven't defined the variable 'maxsize' but you use it none-the-less.

    3) Your 'print' statements in your main function don't make any sense.

    There may be problems in your fillArray() function, but I haven't gone through it thoroughly.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The fillArray function needs a double[], and an int. You're giving it an int and something that isn't even declared.

    Fix that, and look at any others you're getting with it. What do they say, what do they mean? You have to start learning what those messages mean, so you can troubleshoot - that's a big part of programming.

    And Welcome to the forum, mdthejericho!
    Code:
    int fillArray (double[],int);
    float x; 
    
    int main(void)
      
    {
     int n;
     int x; 
       
      n = fillArray (x, maxsize);
    Edit: the variable x can only be ONE thing. You have here as three different things! Ooops!

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    27
    You have made plenty of small and big mistakes. one of them is declaring x before completing the preprocessor commands.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    #define SIZE = 100
    There's no = in #defines, so this should be
    Code:
    #define SIZE 100

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well, on Fridays, I WANT it!!

    There's no = in #defines, so this should be
    We will be French -- Man zee barricades!! Block zee refinery!! We shall get that equal sign into defines, on Fridays!!

    < ROFL >
    Last edited by Adak; 10-22-2010 at 12:39 PM.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define SENTINEL -1
    #define SIZE 100           //my edit: removed equal sign
    
    
    int fillArray (double*,int);
    double x[SIZE];
    
    int main(void)
    {
        int n;
        int maxsize = 20; //sample size
        n = fillArray (x, maxsize);
        printf("%d\n",n);            // my edit: print n
        for (int i = 0; i < SIZE; i++)
            {
            printf("%g\n",*(x+i));   // my edit: print elements of x
            }
        getchar();
        getchar();
        return 0;
    }
    
    
    
    
    
    
    
    
    // Function to read a collection of type float data from
    // the keyboard and store this data in the array x.
    // Returns number of elements read if function completes as // intended.
    // Otherwise: Returns -2 if SIZE <= 0; Returns -1 if file // is empty. Returns 0 if too many items in file (array // is filled while data remains to be processed)
    
    int fillArray
    (double *x, // INOUT: array to be filled
    int arraysize) // IN: size of array
        {
            double item; // Temp cell for each item read
            int count = 0; // Count of number of items read
            if (arraysize <= 0) return (-2);
    
    
            // Execute the "priming" or initial read
            printf("Enter the first data item: ");
            scanf ("%lg", &item);             //my edit: changed %f to %lg
            // Repeat until no more data.
            while (item !=SENTINEL){
            x[count] = item;
            count++;
            if (count >= arraysize) return (0); // array full
            // Execute the "update" or next read
                printf("Enter the next data item: ");
                scanf ("%lg", &item);            //my edit: changed %f to %lg
            } 
            if (count == 0) return (-1); // file empty
            else return (count);
    } // end fillArray
    the code above compiles. fillarray starts filling x at x[1] though.

    edited:
    Last edited by scout; 10-22-2010 at 01:37 PM. Reason: indentation

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    scout: you need to indent your code properly. You pretty much butchered what good indentation there was in mdthejericho's code. It is also a good idea if you summarised what you did.
    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. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    Thanks, guys! I'm glad to be here and plan on learning a lot. I'm going to review all the responses and try some out. I'll post what I come up with.


    Also, the biggest error I got was a constant error for

    Code:
     double x[SIZE];
    It expects a value here and I don't know why. It doesn't matter if I place it in the main program or above the main program.
    Last edited by mdthejericho; 10-22-2010 at 02:13 PM.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    Quote Originally Posted by rags_to_riches View Post
    Code:
    #define SIZE = 100
    There's no = in #defines, so this should be
    Code:
    #define SIZE 100
    THAT WAS IT!!! Yes. Now it compiles and I can try to get the rest of it working. Eventually, I need to write functions that compute the mean and sum.

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    Okay, the code compiles and I can type in numbers, but how do I display all the values in the main program? That's what I was trying to do with those "print" commands that didn't make sense. Here' the new code:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define SIZE  20
         double x[SIZE];
         #define SENTINEL -1
    
    
    
    int fillArray (double[],int);
    
    
    
    int main(void)
      
    {
     int n;
     
       
      n = fillArray (x, SIZE);
      
      
    }  
    
    
    
    
    
    
    
    
    // Function to read a collection of type float data from 
    //    the keyboard and store this data in the array x. 
    // Returns number of elements read if function completes as 
    //    intended.
    // Otherwise: Returns -2 if SIZE <= 0; Returns -1 if file 
    //    is empty.  Returns 0 if too many items in file (array 
    //    is filled while data remains to be processed)
    
    int fillArray 
       (double x[],     // INOUT: array to be filled 
        int arraysize)  // IN: size of array
    {
        double item;  // Temp cell for each item read
        int count;    // Count of number of items read
    
        if (arraysize <= 0) return (-2);
    
        count = 0;    
        // Execute the "priming" or initial read
        printf("Enter the first data item: ");
        scanf ("%f", &item);
        // Repeat until no more data.
        while (item != SENTINEL)
        {
            x[count] = item; 
            count++;        
            if (count >= arraysize) return (0); // array full
    	   // Execute the "update" or next read
            printf("Enter the next data item: ");
            scanf ("%f", &item);
        }
        if (count == 0) return (-1); // file empty
        else return (count);
    } // end fillArray
    sorry for the triple post!

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    Could anyone point me in the right direction?

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > else return (count);
    You store this in n when you return.
    Maybe a for loop to iterate over this range?
    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.

  14. #14
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    Here is my main program and I keep getting the value 0.00 when I try to play around with displaying the array:

    Code:
    int main(void)
      
    {
     int n;
     
       
      n = fillArray (x, SIZE);
      printf("%f", x[1]);
      
      
    }

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Several things need to be tweaked, but start with this. This creates the array in main(), and passes it to the fillArray function. Then displays the data.

    If you create the array in the fillArray function, the array will have they call "local scope", meaning it will die when the program leaves the fillArray function. (there are ways around it, but they are more advanced).

    So:

    Code:
    #include <stdio.h>
    
    #define SIZE 20  //whatever
    
    int main(void)
    {
       int i, n;
       double x[SIZE];
       
      n = fillArray (x, SIZE);
    
      for(i=0;i<SIZE;i++) {
        printf("\n %f", x[i]);
      }
      
      printf("\n\n\t\t\t    press enter when ready");
      (void) getchar(); //hold the console window open
      return 0;  
    }
    Use the above. Question now is are you getting your data OK from the keyboard?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. array copy problem
    By Witch in forum C Programming
    Replies: 3
    Last Post: 03-22-2010, 07:00 PM
  3. Sorting array problem :)
    By BEST in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2009, 01:57 PM
  4. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  5. Replies: 6
    Last Post: 02-15-2005, 11:20 PM