Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PLEASE HELP

Status
Not open for further replies.

CaptRage61

Programmer
Feb 17, 2005
50
US
This is the first C program that I will be writing, it is pretty simple but I can't seem to get started on it, here is what I need to do:
I need to have a user enter 2 numbers that represent the rows and colums of an array. It will then print the numbers starting from 0 at position (1,1) and goes up by 1 for each change in column number and by 10 for each row, ex:
0 1 2 3 4
10 11 12 13 14
20 21 22 23 24....

I can't figure out how to use the printf and scanf functions so the user can enter the values....anyone know how to do this?
 
What have you got so far? Can you post any code you already have? This forum will help with specific problems but we don't write your code for you (specially if its a student posting)

If you're write code on a Unix/Linux platform then
Code:
man printf 
man scanf
are the place to start.

Columb Healy
 
Here is what I have so far, I got the printf and scanf working. Also I am not asking anyone to writh my program for me, just help getting started.

Is there a way in C to have a 2D array, I know in Java it would be array[][].... how would you do this in C?

#include <stdio.h>


int main( ){
int x, y;
printf("enter rows> ");
scanf("%d", &x);
printf("enter columns> ");
scanf("%d", &y);
printf("x is %d\n", x);
printf("y is %d\n", y);
return 0;


}
 
Sorry if I came across a bit strong - we get the odd student trying to cheat on his homework!

You're not actually trying to create an array (which would be int myArray[][];

What you want to look at next is the for loop syntax. For example
Code:
for ( i = 0; i < x; i++ )
  {
  for ( j = 0; j < y; j++ )
    {
    printf ( "%d ", (10 * i) + j + 1 );
    }
  printf ( "\n" );
  }
You would have to have declared i and j as ints.
I hope this helps

Columb Healy
 
Thank you, that did help. I do like your idea of just printing the number instead of storing in an array then printing them.
Thanks
 
C is not very helpful for creating multidimensional arrays on the fly. C++ is much better but that's a different ball game!

If you had to then one option is to use malloc to assign the memory needed. As an example
Code:
int **iArray;
/* iArray is a pointer to an array of pointers */
int x, y, i, j;
/* four intss with meaningless names! */

/*
read in x and y here
*/

/* Allocate the memory */
iArray = malloc ( x * sizeof ( int * ) );
for ( i = 0; i < x; i++ )
  iArray[i] = malloc ( y * sizeof ( int ) );

/* Populate the array */
for ( i = 0; i < x; i++ )
  for ( j = 0; j < y; j++ )
    iArray[i][j] = ( 10 * i ) + j;

/*print it out */
for ( i = 0; i < x; i++ )
  {
  for ( j = 0; j < y; j++ )
    printf ( "%d ", iArray[i][j] );
  printf ( "\n" );
  }

include stdio.h for the printf and sldlib.h for the malloc

I dare say there are gurus out there who could provide cleaner code but I'm demonstrating the principles.

Columb Healy
 
Columb,

I could never understand the malloc function in college. We had an assignment centered around malloc, and I got 15%.

Thank you so very much, it all makes sense now! (A little too late, but better late than never)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top