Array with names

Reply

Join Date: Oct 2004
Posts: 11
Reputation: djextazy is an unknown quantity at this point 
Solved Threads: 0
djextazy djextazy is offline Offline
Newbie Poster

Array with names

 
0
  #1
Oct 5th, 2004
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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 27
Reputation: fahad is an unknown quantity at this point 
Solved Threads: 0
fahad fahad is offline Offline
Light Poster

Re: Array with names

 
0
  #2
Oct 5th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 27
Reputation: fahad is an unknown quantity at this point 
Solved Threads: 0
fahad fahad is offline Offline
Light Poster

Re: Array with names

 
0
  #3
Oct 5th, 2004
Hi,
The declaration should be like this
char X[][];

Fahad
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 11
Reputation: djextazy is an unknown quantity at this point 
Solved Threads: 0
djextazy djextazy is offline Offline
Newbie Poster

Re: Array with names

 
0
  #4
Oct 5th, 2004
Originally Posted by fahad
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 "<<i<<"-st name: "; cin>>a[i]; }

Correct me please if I am mistakening. Thank you for your help!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 27
Reputation: fahad is an unknown quantity at this point 
Solved Threads: 0
fahad fahad is offline Offline
Light Poster

Re: Array with names

 
0
  #5
Oct 5th, 2004
Hi,
Yes u r right ,
try this............and if still getting some problems discuss it again
Fahad
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Array with names

 
0
  #6
Oct 5th, 2004
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 )
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 11
Reputation: djextazy is an unknown quantity at this point 
Solved Threads: 0
djextazy djextazy is offline Offline
Newbie Poster

Re: Array with names

 
0
  #7
Oct 5th, 2004
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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 11
Reputation: djextazy is an unknown quantity at this point 
Solved Threads: 0
djextazy djextazy is offline Offline
Newbie Poster

Re: Array with names

 
0
  #8
Oct 5th, 2004
Originally Posted by Dave Sinkula
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:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 714
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Array with names

 
0
  #9
Oct 5th, 2004
>It works fine as for(i=1;i<=n;i++) .
Yes it does. But then again, so does this:
  1. i = 9;
  2. loopie:
  3. // Do stuff
  4. if ( --i > 9 - n )
  5. 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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Array with names

 
0
  #10
Oct 5th, 2004
Originally Posted by djextazy
It works fine as for(i=1;i<=n;i++) . Trust me. :lol:
Do you see the bug here?
  1. #include <stdio.h>
  2.  
  3. #define N 10
  4.  
  5. int main()
  6. {
  7. int i, array[N] = {1,2,3,4,5,6,7,8,9,10};
  8. for ( i = 1; i <= N; ++i )
  9. {
  10. printf("array[%d] = %d\n", i, array[i]);
  11. }
  12. return 0;
  13. }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC