Help with array please

This is a discussion on Help with array please within the C Programming forums, part of the General Programming Boards category; I'm doing a complex homework assignment, so I decided to try and break it up into steps so I can ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    6

    Help with array please

    I'm doing a complex homework assignment, so I decided to try and break it up into steps so I can figure it out easier but even the small steps are turning out to be very difficult for me.

    My problem is I can't figure out how to populate this array with a unknown size. Additonally I am a very noob at this programming and am having trouble correcting these errors. Could someone please help me? Thanks.

    Code:
    //Populating Array
    
    
    //Printing the Array
    
    
    #include <stdio.h>
    void printArray(int);
    
    
    int main(void)
    
    
    {
        char more_to_process;
        int num[], i=0;
    printf("Are there any more numbers to be converted?\n");
        printf("Please type Y to continue and N if you want to stop \n");
        scanf("%c", &more_to_process);
        while ((more_to_process != 'Y') && (more_to_process != 'N'))
    {
        printf("Please type Y to Continue and N if you want to stop \n");
        scanf("%c", &more_to_process);
        }
        
        if (more_to_process = 'Y'){
            printf("\nENTER THE NUMBER\n");
            scanf("%d",num[i]);     
            
                    else
            for(x = 0;x <= i; x++){
                printArray(num[x])
            }
        }
    }    
        
            printArray(int num)
        { 
        printf("%d\n",num);
        }

    ​ERRORS

    Array1.c: In function 'main':
    Array1.c:12: error: array size missing in 'num'
    Array1.c:26: error: expected '}' before 'else'
    Array1.c:27: error: 'x' undeclared (first use in this function)
    Array1.c:27: error: (Each undeclared identifier is reported only once
    Array1.c:27: error: for each function it appears in.)
    Array1.c:29: error: expected ';' before '}' token
    Array1.c: At top level:
    Array1.c:31: error: expected identifier or '(' before '}' token
    Array1.c:33: warning: conflicting types for 'printArray'
    Array1.c:6: warning: previous declaration of 'printArray' was here


  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    23
    If you need to populate an array who has an unknown size, you may need to use dynamic memory allocation (dynamic arrays).

    To declare the array as dynamic : int* num (this may solve some of your compilation problems).

    To see how to allocate dynamic memory, check this out:
    Dystopian Code: Dynamic Multidimensional Arrays in C

    After you allocated the memory you can use like a "normal" (static memory) array.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    Code:
    {	char more_to_process;
    	int* num[], i=0;
    printf("Are there any more numbers to be converted?\n");
    	printf("Please type Y to continue and N if you want to stop \n");
    	scanf("%c", &more_to_process);
    	while ((more_to_process != 'Y') && (more_to_process != 'N'))
    {
    	printf("Please type Y to Continue and N if you want to stop \n");
    	scanf("%c", &more_to_process);
    	}
    	
    	if (more_to_process = 'Y'){
    		printf("\nENTER THE NUMBER\n");
    		scanf("%d",num[i]); 	
    		
    				else
    		for(x = 0;x <= i; x++){
    			printArray(num[x])
    		}
    	}
    }	
    	
    		printArray(int num)
    	{ 
    	printf("%d\n",num);
    	}
    Thats what I did same exact errors. Even more confused. This is my first time using arrays in C. I am not very good at this.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    If you're just learning arrays, then I'd suggest you start with statically defined arrays.
    Code:
    int num[100] ;
    or even
    Code:
    #define ARR_SIZE 100
    
    int num[ARR_SIZE] ;
    ...
    //and for loop to populate array, read array, print array, etc.
    for( i = 0 ; i < ARR_SIZE ; ++i )
    {...}

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    Above is understandable ty though Tclausex ; like I said my program has to use input a array with a unknown length.. Trying to put what I just said in proper english is difficult; and that is my main source of current troubles.

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Code:
    if (more_to_process = 'Y'){
    That is an assignment, not a comparison. = for assignment, == for comparison.

    Code:
    int *array = malloc(numItems * sizeof(*array));
    int i = 0;
    for(; i < numItems; ++i) {
        array[i] = i + 1;
    }
    
    for (i = 0; i < numItems; ++i) {
        printf("Index: %d Value: %d\n", i, array[i]);
    }
    free(array);

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Certainly not the way I would introduce students to arrays, but what do I know.

    dheaven's link and Dynamic Memory Allocation, Part 1 should help you.

    In any case, you'll at least want to determine from the user how many integers are to be entered. If reading from a file, you could count the number of integers, then do the memory allocation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  2. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  3. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  4. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM

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