954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Is This an Array Program???

just want to ask if this is an array program????...
my professor told us to make array program that displays 10 names..
my professor tells us that we can use C or C++ program..
i prefer using C cause im much familiar with C programs...

Output:
fdgdfg
dfgdfg
dfgdfg
dfgdfg
dfgdfgd
dfgdfg
dfgdfgd
dfgdfgd
dfgdgdf
dfgdfg


this is what i did:

#include
#include
#include

char names[10];
int n;

void main()
{
clrscr();
printf("Input 10 Names:\n");
for(n=0;n<10;n++){
scanf("%s",n);}
getch();
}

the program produces output which i inputed.
i think its ok..
but im not sure if this is an Array program..
i think i just use the basic C program...
and also, i think i had just used for loop and nothing else...
do you think i just used a simple C program that display names without having any for loops or Arrays???...

please help me.. im confuse...

mccbebz
Newbie Poster
5 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

>i prefer using C cause im much familiar with C programs..
Clearly you aren't.

>#include
Please don't waste your time with this garbage.

>#include
Why include a header that you're not using?

>char names[10];
>int n;
Global variables should be avoided. Also, names is poorly named as it defines a single array when you probably wanted an array of arrays.

>void main()
int main ( void )

>clrscr();
Unnecessary, non-portable, anti-social, need I go on?

>scanf("%s",n);}
n is not your array, names is. Also, using %s with scanf is equivalent to gets, which is well known to be a huge mistake. Use the field width modifier to make sure that you don't overflow the array.

>getch();
There are portable ways to do this.

Let's look at the fixed code:

#include<stdio.h>

int main ( void )
{
  char names[10][20];
  int i;

  printf ( "Input 10 Names: " );

  for ( i = 0; i < 10; i++ )
    scanf ( "%19s", names[i] );

  /* Clean up the stream */
  while ( getchar() != '\n' )
    ;

  /* Pause */
  getchar();

  return 0;
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

thakz anyway... have a nice day... just want to ask.. why is there a "19" in your scanf("19%s",names[i]);?..

im just a biggener to this kind of subject..

mccbebz
Newbie Poster
5 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

>>char names[10][20];
for practical purposes this means names is an array that can hold up to 10 names. Each name can have up to 19 visible char and a terminal null char to make it a null terminated char array instead of just a char array.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

ok tnx so much... what about the " char[10][20] " whats the porpuse of putting [20]?.... im quite confiuse on this one..

mccbebz
Newbie Poster
5 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

>what about the " char[10][20] " whats the porpuse of putting [20]?
char a[20] gives you an array of 20 characters, right? What if you want ten of them? Sure, you could do this:

char a[20];
char b[20];
char c[20];
char d[20];
...

But that reminds you of the same problem that's solved by making an array of characters. So you add another dimension to the array and make it an array of 10 arrays of 20 characters:

char a[10][20];

If it helps, you can look at an array as a vector:

[0][1][2][3][4][5][6][7][8][9]

And a two dimensional array as a table:

[0,0][0,1][0,2][0,3][0,4][0,5]...[0,18][0,19]
[1,0][1,1][1,2][1,3][1,4][1,5]...[1,18][1,19]
[2,0][2,1][2,2][2,3][2,4][2,5]...[2,18][2,19]
[3,0][3,1][3,2][3,3][3,4][3,5]...[3,18][3,19]
[4,0][4,1][4,2][4,3][4,4][4,5]...[4,18][4,19]
[5,0][5,1][5,2][5,3][5,4][5,5]...[5,18][5,19]
[6,0][6,1][6,2][6,3][6,4][6,5]...[6,18][6,19]
[7,0][7,1][7,2][7,3][7,4][7,5]...[7,18][7,19]
[8,0][8,1][8,2][8,3][8,4][8,5]...[8,18][8,19]
[9,0][9,1][9,2][9,3][9,4][9,5]...[9,18][9,19]

A three dimensional array would be a cube, and a four dimensional array would be a vector of cubes, then a five dimensional array would be a table of cubes, and so on. But you rarely need more than two dimensions. ;)

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
>>char names[10][20]; for practical purposes this means names is an array that can hold up to 10 names. Each name can have up to 19 visible char and a terminal null char to make it a null terminated char array instead of just a char array.


ya it's string example

harsh.varudkar
Newbie Poster
1 post since May 2010
Reputation Points: 5
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You