Link to home
Start Free TrialLog in
Avatar of rockmagick
rockmagick

asked on

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?

Thank you for your time.

Avatar of redpirk
redpirk

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
your storing.
ASKER CERTIFIED SOLUTION
Avatar of sumant032199
sumant032199
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rockmagick

ASKER

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.
Wait, let me think upon this.

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
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;
}