Thread: Undefined Number of Contents in Array

  1. #1
    Registered User
    Join Date
    Mar 2008

    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:

    #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
    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()?
    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

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

    you probably want

    for ( comp_values = 1;

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    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 . . . .

    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:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

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