Thread: User defined 2-D array problem

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    14

    User defined 2-D array problem

    I have to create a program that will have the user define what size the rows and columns are and a starting number. The array will then print out the array starting with the starting number and ending with the ending number, which will be defined as the columns and rows multipled together, plus the starting number. Something like this:

    Enter starting value: 50
    Enter row size: 5
    Enter # of rows: 7
    50 51 52 53 54
    55 56 57 58 59
    60 61 62 63 64
    65 66 67 68 69
    70 71 72 73 74
    75 76 77 78 79
    80 81 82 83 84

    So far I have:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    void print2data(int *p, int rows, int cols, int start);
    
    int main(void)
    {
    
    	int cols, rows, start;
    	int *p;
    
    	printf("Enter starting value: ");
    	scanf("%d", &start);
    	printf("Enter row size: ");
    	scanf("%d", &rows);
    	printf("Enter # of rows: ");
    	scanf("%d", &cols);
    
    
    	p = (int*)malloc (rows * cols * sizeof(int));
    
    	print2data(p, rows, cols, start);
    
    	system("PAUSE");
    }
    
    
    void print2data(int *p, int rows, int cols, int start)
    {
    	
    	int i, j, end, last;
    
    	end = (rows * cols) - 1;
    	printf("rows*cols = %d\n", end);
    	printf("start number is %d\n", start);
    	rows = start;
    	cols = start;
    	last = start + end;
    
    	printf("array is: \n");
    	for (i= 0; i <= rows; i++)
    	{
    		for(j = 0; j <= cols; j++)
    		{
    			printf("%d ", *p);
    			p++;
    		}
    		printf("\n");
    	}
    }
    I'm just frustrated with the for statements in the print2data function. I know it's messed up right now, but I keep getting garbage values, and I feel the more I keep writing into the program, the farther I'm getting. Thanks for any help.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Arrays and memory from pointers are indexed starting at 0, which means you should not be reading <= to the rows and columns but rather <.

    Another point. You're surprised you're getting garbage values, but you never actually set the values of the elements of the block of memory you allocated. That should be basic knowledge. Memory that you allocate in any form is uninitialized by default, which means it could have any random value. An exception would be calloc() which explicitly sets all of the bits in the memory block to 0.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    Im not really surpized I was gettin garbage, I just got frustrated with what I kept getting, so I rewrote the print2data from scratch. I know it's off, but I figured starting fresh would be better.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    void print2data(int *p, int rows, int cols, int start);
    
    int main(void)
    {
    
    	int cols, rows, start;
    	int *p;
    
    	printf("Enter starting value: ");
    	scanf("&#37;d", &start);
    	printf("Enter row size: ");
    	scanf("%d", &rows);
    	printf("Enter # of rows: ");
    	scanf("%d", &cols);
    
    
    	p = (int*)malloc (rows * cols * sizeof(int));
    
    	print2data(p, rows, cols, start);
    
    	system("PAUSE");
    }
    
    
    void print2data(int *p, int rows, int cols, int start)
    {
    	
    	int i, j, end, last;
    
    	end = (rows * cols);
    	rows = end;
    	last = start + end;
    
    	printf("rows*cols = %d\n", end);
    	printf("start number is %d\n", start);
    
    	printf("array is: \n");
    	for (i = start; i < end; ++i)
    	{
    		printf("%d ", i);
    	}
    		for(j = start; j < last; ++j)
    		{
    			printf("%d ", j);
    		}
    
    		
    		printf("\n");
    
    }
    I'm starting to get non garbage numbers that are correct, but they are misplaced. Not sure were to go from here. Any help would be great, thanks.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You didn't read my original reply the way I had intended it. I'll try again.

    Code:
    p = (int*)malloc (rows * cols * sizeof(int));
    This allocates a block of memory with garbage in it.

    Code:
    print2data(p, rows, cols, start);
    This prints a block of memory with garbage in it.

    Hint: Insert a step where you overwrite the garbage before you print it.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Also, read the FAQ entry on why casting malloc in C is a bad idea.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By nashsclay in forum C Programming
    Replies: 4
    Last Post: 05-03-2008, 02:34 PM
  2. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  3. User Defined Array
    By jerez_z in forum C Programming
    Replies: 27
    Last Post: 06-03-2004, 11:23 AM
  4. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  5. help w/ array problem
    By rhythm313 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2002, 12:12 AM