Thread: can't populate my array

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    2

    can't populate my array

    whats happening with my array !!

    i cant print my array and as well as my c is force stopping (crashing)

    stat.c

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Hint: compile with warnings turned on.

    Bye, Andreas

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    That's an inserting way to do it. Note that your code is only 95 lines long, which is certainly short enough to post directly in your post, inside [code][/code] tags, especially if you leave out the commented out code.
    That will get far more views, and probably quicker help.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    In populatearray():
    How many times do you think that you can append prompt to a 3 char buffer ?

    The header file is called <string.h>

    For more warnings increase the warning level of your compiler
    Kurt

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    2
    Code:
    /* * program: stats in c
    * displays sum , mean, max
    */
    # include <stdio.h>
    # include <strings.h>
    # include <math.h>
    
    
    typedef struct my_string
    {
      char str[256]; // my string contains an array of 255 characters + null
    } my_string;
    
    
    my_string read_string(const char* prompt)
    {
      my_string result;     // declares a "my_string" variable (contains the array of character)
      printf("%s", prompt); // output the string from the prompt "%s" defines where to place the string in the output
      scanf(" %255[^\n]", result.str ); // scan the input looking for upto 255 characters [ that are not newlines ], read this into the string variable
      return result; // return the my string value
    }
    
    
    
    
    double readdouble(const char*prompt)
    {
      my_string input ;
      double result;
      printf("%s", prompt);
      while (scanf("%lf", &result)!=1)
      {
        scanf("%*[^\n] ");
        printf("Enter a number: \n");
        printf("%s",prompt);
      }
      return result;
    }
    
    
    double  read_double_range(int min, int  max, const char*prompt)
    {
      double result = readdouble(prompt);
    
    
      while (result < min || result > max)
      {
        printf("Please Enter a value : %lf, %lf\n", min, max );
        result = readdouble(prompt);
      }
      return result;
    }
    
    
    void populatearray(double numbers[], int value)
    {
      int i;
      char prompt[17] = "";
      char buffer[3] = "";
      
      for(i=0;i<value; i++)
      {
        strncpy(prompt, "Enter value ", 13);
        sprintf(buffer, "%d", (i+ 1) % 100);
        strncat(prompt, buffer, 2);
        strncat(prompt, ":", 2);
        
        numbers[i] = readdouble(prompt);
      }
    }
    
    
    void printarray(const double numbers[], int value)
    { 
      int i;
      for (i = 0; i < value; i++)
      {
        printf("You Entered:%4.2f\n",numbers[i]);
      }
    }
    
    
    void swap(double v1, double v2)
    {
     double tempstore;
     tempstore = v1;
      v1 = v2;
      v2 = tempstore;
    }
    
    
    
    
    void sort(const double numbers[], int value)
    {
      int i, j;
      for (i > value; i = 0;i--)
      {
         for (j = 0; j < i-1;i++) 
          {
            if (numbers[j] > numbers [j+1])
              {
                swap(numbers[j], numbers[j+1]); 
              }
          }
        
      }
    }
    
    
    
    
    double Sum (const double numbers[], int value)
    {
      int i;
      double result = 0;
      for (i = 0; i < value; i++)
      {
        result += numbers[i];
      }
      return result;
    }
    
    
    double max(double numbers[], int value)
    {
      int i;
      double result;
      result = numbers[0];
      for (i = 0; i < value; i++)
      {
        if (result < numbers[i])
        {
          result = numbers[i];
        }
      } 
      return result;
    }
    
    
    double Mean(const double numbers[], int value)
    {
      double result;
      result = Sum(numbers, value);
      return result/ value;
    }
    
    
    double variance(const double numbers[], int value)
    {
      int i ;
      double calcvar, avg, result;
      calcvar = 0;
      avg = Mean(numbers,value);
      for (i = 0; i < value; i++)
        {
          calcvar += (numbers[i] -  avg) * (numbers[i] -  avg);
        }
      
      result = calcvar / (value - 1);
      return result;
    }
    
    
    
    
    
    
    
    
    int main()
    {
      int value;
      double sum, maximum, average, Variance;
      printf("How many numbers would you like to use?");
      scanf("%d",&value);
      double numbers[value]; // set length
      populatearray(numbers, value);
       sort(&numbers[value], value);
      printarray(numbers, value);
      sum = Sum(numbers, value);
      printf("sum is: %4.2lf\n", sum);
      maximum = max(numbers, value);
      printf("Maximum value is: %4.2lf\n", maximum);
      average = Mean(numbers, value);
      printf("Mean value is: %4.2lf\n", average);
      Variance = variance(numbers, value);
      printf("Variance value is: %4.2lf\n", Variance);
    
    }
    it compiles fine but its is not swapping i mean it is not going in order so let say
    i enter 2, 1
    then i should come up with you entered 1 and then 2 but instead it comes like you entered 2 and 1
    Last edited by k31453; 05-20-2013 at 03:42 AM.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,673
    For the third time, increase the warning levels in your compiler!!! Then, fix those warnings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Populate An Array With Numbers
    By mcertini in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2011, 11:35 AM
  2. populate array problem
    By DaleZ in forum C# Programming
    Replies: 3
    Last Post: 11-24-2010, 01:41 AM
  3. populate an array struct
    By flipguy_ph in forum C Programming
    Replies: 10
    Last Post: 04-17-2009, 04:07 PM
  4. FOR loop to populate a 2D array
    By Swerve in forum C++ Programming
    Replies: 7
    Last Post: 03-05-2008, 10:45 AM
  5. Wish to populate a 2D array using pointers
    By hamsteroid in forum C Programming
    Replies: 14
    Last Post: 10-24-2007, 10:28 AM