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

Array with names

As this is my first post on this forum I want to say "Hello!" to everybody and I hope that we'll help each other as much as we can and we'll be very good friends. :)

So, this is my problem ( a rather simple one but I don't know how to resolve it ) : I found a exercise that wants me to have a array with some student's names and another array with their grades ( numerical grades ) at informatics. :eek:

Example: X=('John','Michelle','Jack','Robert'); Y=(10,5,6,7);

My question is: how do I put those names in the array so that a name ( let's say John ) should occupy a single position? For example: X[1]= 'John'; X[2]='Michelle' , etc. I have to read them from the keyboard , of course. :)

Thank you very much for your time and eventual help! ;)

djextazy
Newbie Poster
11 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

Hi djextazy,
Well come to DaniWeb ,
if u want to have full name at one location of the array
u have to declare array as a two dimensional array,
i-e its declaration would be like this
char [][];
or u can also use array of pointers , but if u r new to C++ u should use
two dimension array
I hope it resolved ur issue but still if their is some problem feel free to ask again.
Fahad

fahad
Light Poster
27 posts since Sep 2004
Reputation Points: 14
Solved Threads: 0
 

Hi,
The declaration should be like this
char X[][];

Fahad

fahad
Light Poster
27 posts since Sep 2004
Reputation Points: 14
Solved Threads: 0
 

Hi, The declaration should be like this char X[][];

Fahad

Do you mean something like
a[1][1] = "Victor";
a[2][2] = "Jose"; ???

I mean:
char a[30][30],i,n;
cout<<"n= "; cin>>n;
for(i=1;i<=n;i++) { cout<<"Give "<>a[i]; }

Correct me please if I am mistakening. Thank you for your help!

djextazy
Newbie Poster
11 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

Hi,
Yes u r right ,
try this............and if still getting some problems discuss it again
Fahad

fahad
Light Poster
27 posts since Sep 2004
Reputation Points: 14
Solved Threads: 0
 

An array of size N will be indexed from 0 to N-1. The standard idiomatic for loop is then like this:

for ( int i = 0; i < N; ++i )
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

It works , altought the exercise says that I should use two arrays , one for the names and one for the grades. Oh well , it's important that it works ... :)

Thank you very much!

djextazy
Newbie Poster
11 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

An array of size N will be indexed from 0 to N-1. The standard idiomatic for loop is then like this:

for ( int i = 0; i < N; ++i )

It works fine as for(i=1;i<=n;i++) . Trust me. :lol:

djextazy
Newbie Poster
11 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

>It works fine as for(i=1;i<=n;i++) .
Yes it does. But then again, so does this:

i = 9;
loopie:
  // Do stuff
  if ( --i > 9 - n )
    goto loopie;

Most programmers prefer to use the idiomatic approach because it's easier to use, easier to get used to, and common enough that you don't have to get used to something else everytime you read a different person's program.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
It works fine as for(i=1;i<=n;i++) . Trust me. :lol:

Do you see the bug here? ;)

#include <stdio.h>
 
 #define N 10
 
 int main()
 {
    int i, array[N] = {1,2,3,4,5,6,7,8,9,10};
    for ( i = 1; i <= N; ++i )
    {
 	  printf("array[%d] = %d\n", i, array[i]);
    }
    return 0;
 }
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Do you see the bug here? ;)

#include <stdio.h>
 
 #define N 10
 
 int main()
 {
    int i, array[N] = {1,2,3,4,5,6,7,8,9,10};
    for ( i = 1; i <= N; ++i )
    {
 	  printf("array[%d] = %d\n", i, array[i]);
    }
    return 0;
 }

Hehe. :) It should be like this:

for(i=1;i<=n;i++)

NOT

for(i=1;i<=n;++i)


The second one is wrong because i increases with 1 BEFORE entering the for .. so i practically starts with the value 2. While in my example , i starts from 1 and ends at 10 , meaning exactly what it has to do. ;) :eek:

djextazy
Newbie Poster
11 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

Hehe. :) It should be like this:

for(i=1;i<=n;i++)

NOT

for(i=1;i<=n;++i)

The second one is wrong because i increases with 1 BEFORE entering the for .. so i practically starts with the value 2. While in my example , i starts from 1 and ends at 10 , meaning exactly what it has to do. ;) :eek:

No, the second one is not wrong, and your explanation is incorrect. I take it you didn't even try it.:rolleyes:

And I take it that you still don't see the bug. This is exactly why you shouldn't be doing this.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
No, the second one is not wrong, and your explanation is incorrect. I take it you didn't even try it.:rolleyes: And I take it that you still don't see the bug. This is exactly why you shouldn't be doing this.

Indeed , I haven't tested them until now. It seems like both versions give the same result ...

My guess is: i should start from 0? :p

djextazy
Newbie Poster
11 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 
Indeed , I haven't tested them until now. It seems like both versions give the same result ... My guess is: i should start from 0? :p

Well, yes, but the bug is that you should not attempt to accessarray[10] because it is beyond the array bounds.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

>My guess is: i should start from 0?
If you have to guess, don't be so quick to answer questions because you'll probably be wrong. Even if you bother to test your assumptions and they pan out, you could still be wrong on something more subtle.

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

hi
look if u want array and every case on array it's name look:
#include
int main()
{char *a[5]={"sami","rami","wael","dani","nami"};
for (i=0;i<5;i++)
printf("%s",a[i]);
return 0;
}

mohammad
Newbie Poster
5 posts since Sep 2004
Reputation Points: 10
Solved Threads: 0
 

--------------------------------------------------------------------------------
hi
look if u want array and every case on array it's name look:
#include
int main()
{char *a[5]={"sami","rami","wael","dani","nami"};
for (i=0;i<5;i++)
printf("%s",a[i]);
return 0;
}

mohammad
Newbie Poster
5 posts since Sep 2004
Reputation Points: 10
Solved Threads: 0
 
-------------------------------------------------------------------------------- hi look if u want array and every case on array it's name look: #include int main() {char *a[5]={"sami","rami","wael","dani","nami"}; for (i=0;i<5;i++) printf("%s",a[i]); return 0; }

I want it with #include and all the rest ( cout & cin) . This one doesn't work:

#include <iostream>
#include <conio.h>
using namespace std;
main()
{ char *a[5]; int i;
for(i=0;i<5;i++) { cout<<"Dati numele #"<<i+1<<": "; cin>>a[i]; }
for(i=0;i<5;i++) cout<<a[i]<<endl;
getch();
}


It freezes after I input 2 names. :rolleyes: What should I do?

djextazy
Newbie Poster
11 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 
char *a[5];


An array of uninitialized pointers. These are not automagically strings, you need to have them actually point to some memory you can write to before you try to write to it. Or actually declare an array for strings.

char a[5][80];
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
char *a[5];

An array of uninitialized pointers. These are not automagically strings, you need to have them actually point to some memory you can write to before you try to write to it. Or actually declare an array for strings.

char a[5][80];

I actually worked out the exercise with a double array , as you said ( a[5][80 ). But I was just churious about mohammad's version . :rolleyes:

djextazy
Newbie Poster
11 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

Post: Markdown Syntax: Formatting Help
You