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.