• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

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.

0
rockmagick
Asked:
rockmagick
  • 3
  • 2
1 Solution
 
redpirkCommented:
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.
0
 
sumant032199Commented:
Since you are asking about "struct"
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)
{
       struct Names *head,*temp;
       int n,i;
       printf("\nEnter Number :");
       scanf("%d",&n);

       head=(struct Names *) malloc(sizeof(struct Names));
       printf("\nEnter Name : ");
       scanf("%s",head->name);
       head->next=NULL;
       temp=head;

       for(i=1;i<=n;i++)
      {
          head->next=(struct Names *) malloc(sizeof(struct Names));
          head=head->next;
          printf("\nEnter Name : ");
          scanf("%s",head->name);
          head->next=NULL;
      }
       head=temp;
       while(head)
       {
          printf("\n%s",head->name);
          head=head->next;
       }
       return 0;
}
0
 
rockmagickAuthor 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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
sumant032199Commented:
Wait, let me think upon this.
0
 
redpirkCommented:

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
 
sumant032199Commented:
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now