• C

# multidimensional arrays

In basic computer language I'd use a for next loop to get x number of
players and placed them in their own variable, but I can't find out how to
do this in C!

Here's a sample in basic:

10 input"Number of players";pl
20 for a = 1 to pl
30 print"Name of player ";a
40 input A\$(a)
50 next a

This would give me values for
A\$(1)="Tom"
A\$(2)="Dick"
A\$(3)="Harry"
and so on...

I will not know the number of players beforehand.  It will be inputed during the program.  Does struct have something to do with it?

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
if there is a max number of availible players, it's easiest
to define a 2d array:

char playernames[x][y];

otherwise, you'll have to dynamically allocate the memory for
the names of the players, which is probably simplest to do in c++.
if you dont want or can use ++ you can make a linked list, for
0
Commented:
I think linked list will make you happy. Linked is a most sophisticated technique for this kind of problems.
Array is out of question because they require fixed size to be mentioned.
Another way is using realloc() function but that is not required. Here is a simple code which show how to use a linked list for your problem.
#include <stdio.h>
#include <alloc.h>

struct Names
{
char name[15];
struct Names *next;
};
int main(void)
{
int n,i;
printf("\nEnter Number :");
scanf("%d",&n);

printf("\nEnter Name : ");

for(i=1;i<=n;i++)
{
printf("\nEnter Name : ");
}
{
}
return 0;
}
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
The program runs but when I enter 3 for a number of players, it asks for 4 names.  I added a -1 in this line:
for(i=1;i<=n-1;i++)
It nows asks for the right number.  Is this going to 'screw up' anything?

Also it is still to cryptic for me to understand.  Trying to figure out how to distinguish between players when I want to later on.

How can I specify a certain player (1,2, or 3) later on?  I know in arrays I can just add a name(1) in the ()'s, but how do I do it in this program?  Haven't gotten to C++ yet.  Still learning C.
0
Commented:
Wait, let me think upon this.
0
Commented:

One way to distinguish between players would be to add more
variables to the structure, ie a

int player;

which is incremented for each player.

The you make a function, for example

char * getname(int plr)
{
inr i=0;
struct Names* tmp;

tmp=head; // gotta make sure the first entry is availible
while(tmp->player!=plr)
{
tmp=tmp->Next;
if(!tmp)
return(NULL);
}

return(tmp->name);
}

something like that.. im writing out og head, so im not sure its 100% correct..

the problem with this is if you have alot of players and want to get the names often,
because this is a slow way to do it...

/L
0
Commented:
This whole program has become somewhat tedious because I have restricted
my self to use minimum amount of memory. This code I am presenting you can
be certainly improved.

#include <stdio.h>
#include <alloc.h>
#include <string.h>

#define MAX_LENGTH_OF_NAME 15

void get_name(char *c,char *whole,unsigned int n);
int main(void)
{
char *names,temp[MAX_LENGTH_OF_NAME];
/* names stores the complete of names you want. The size of
pointer grows as per needed using realloc function*/

int n,i;
printf("\nEnter Number : ");
scanf("%d",&n);

printf("\nEnter 1th Name : ");
scanf("%s",temp);
names=(char *) malloc(strlen(temp)+1);
strcpy(names,temp);

for(i=2;i<=n;i++)
{
printf("\nEnter %dth Name : ",i);
scanf("%s",temp);
names=(char *) realloc(names,strlen(temp)+1);
strcat(names," ");
/*  Every name is seperated from other by a space
This is because the program uses minimum memory.
*/
strcat(names,temp);
}
strcat(names," ");
for(i=1;i<=n;i++)
{
get_name(temp,names,i);
/*
get_name() is function in which you give the complete names
pointer and i as a integer to return i th name into temp.
*/
printf("\n%dth name = %s",i,temp);
}
return 0;
}
void get_name(char *c,char *whole,unsigned int n)
{
char *w;
int i;
w=whole;
i=1;
while(i<=n-1) /* Skips first names and keeps count of spaces */
{
if(*w==' ') i++;
w++;
}
i=0;
while(*w!=' ') /* Puts the name into pointer c.*/
{
c[i]=*w;
w++;
i++;
}
c[i]='\x0';      /* terminated by null character. */
return c;
}
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.