Undefined Number of Contents in Array

This is a discussion on Undefined Number of Contents in Array within the C Programming forums, part of the General Programming Boards category; I am trying to write a program in C to calculate the mean of a set of numbers. Of course, ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    1

    Undefined Number of Contents in Array

    I am trying to write a program in C to calculate the mean of a set of numbers. Of course, the number of values is up to the user, so I have a variable no_values that will get the number of values by scanf and an array values_x for variables and another values_f for frequency. I have scanned the chapter in my Programming in C book by Stephen Kochan but can't find this stuff. Here's the source code:

    Code:
    #include <stdio.h>
    
    int main () {
    	printf("\n\nAn Application to find the mean of a set of numbers");
    
    	int no_values, comp_values;
    
    	printf("\n\nHow many values will you be entering? ");
    	scanf("%i", &no_values);
    
    	comp_values = no_values -1;
    	int array_values;
    	int values_x[array_values];
    	int values_f[array_values];
    
    	printf("\nPlease start entering your values");
    
    	printf("|  x    |  f    |\n\n");
    
    	for ( comp_values == 1; comp_values < no_values; ++comp_values ) {
    
    		printf("x Variable: ");
    		scanf("%i", &values_x[array_values]);
    
    		printf("f Frequency: ");
    		scanf("%i", &values_f[array_values]);
    
    		printf("|%i    |%i    |\n\n", values_x, values_f);
    	}
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,161
    It looks like you are trying to use variable length arrays, but array_values was not initialised.

    Have you reached the point where the book talks about dynamically allocated arrays with malloc() and free()?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    >for ( comp_values == 1;

    you probably want

    for ( comp_values = 1;

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    int main () {
    	printf("\n\nAn Application to find the mean of a set of numbers");
    
    	int no_values, comp_values;
    Note that declaring variables in the middle of blocks -- after, say, printf() statements -- is C99, not C89. You should probably avoid it if possible.

    Then again, variable length arrays are C99 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 02:31 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM

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