943,469 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 7504
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 5th, 2004
0

Array with names

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djextazy is offline Offline
11 posts
since Oct 2004
Oct 5th, 2004
0

Re: Array with names

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
Reputation Points: 14
Solved Threads: 0
Light Poster
fahad is offline Offline
27 posts
since Sep 2004
Oct 5th, 2004
0

Re: Array with names

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

Fahad
Reputation Points: 14
Solved Threads: 0
Light Poster
fahad is offline Offline
27 posts
since Sep 2004
Oct 5th, 2004
0

Re: Array with names

Quote 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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djextazy is offline Offline
11 posts
since Oct 2004
Oct 5th, 2004
0

Re: Array with names

Hi,
Yes u r right ,
try this............and if still getting some problems discuss it again
Fahad
Reputation Points: 14
Solved Threads: 0
Light Poster
fahad is offline Offline
27 posts
since Sep 2004
Oct 5th, 2004
0

Re: Array with names

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 )
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 5th, 2004
0

Re: Array with names

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djextazy is offline Offline
11 posts
since Oct 2004
Oct 5th, 2004
0

Re: Array with names

Quote 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:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djextazy is offline Offline
11 posts
since Oct 2004
Oct 5th, 2004
0

Re: Array with names

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 5th, 2004
0

Re: Array with names

Quote 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. }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: hello world
Next Thread in C Forum Timeline: help me out on opencv





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC