• C

arrays

I am setting up an array that will list the first and last name of someone as well as a score they received on a test. Do I define it as follows so that it is a list of ten names with no more than 14 characters?

int name [10] [14];

Do I write the array as follows to print out the full name all together?

name [0] [0] = 'J';
name [0] [1] = 'O';

Do I need to have seperate arrays to print out the highest, lowest, and average scores?
cbrookhartAsked:
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.

AlexVirochovskyCommented:
No, next is example of better solution:
char *names[10];
names[0] = strdup("Vasia      ");
or
names[0] = new char[15];
or
names[0] = calloc(15, 1);

and don't forget in end free of memory .
I hope, it helps. Alex
0

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial
cbrookhartAuthor Commented:
I meant to ask this as well. Can I use the arrays with the stdio.h header file or do I also need to include the string header file?
0
ozoCommented:
You don't need any header files to use arrays.
You should include <string.h> to use strdup
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

hongjunCommented:
I will come back to you later if you have evaluated AlexVirochovsky's answer.
0
cbrookhartAuthor Commented:
I have added more to the program and I have now compiled it. I ended up with 22 errors and 21 warnings. What have I done wrong in this program?
#include <stdio.h>
#include <string.h>
int main (void)
int i
char name [10] [15];
int score [10];
{
      for (i=0; i < 10; score[i++]=rand());
            printf("%s\n", name[0]);
      strcpy(name[0], "Mary Doe");
      strcpy(name[1], "Peter Chi");
      strcpy(name[2], "Dean Pet");
      strcpy(name[3], "John Lee");
      strcpy(name[4], "Sue Smith");
      strcpy(name[5], "Helen Di");
      strcpy(name[6], "Keith Pot");
      strcpy(name[7], "Sam Spade");
      strcpy(name[8], "Jane Run");
      strcpy(name[9], "Linda Brown");
      {
      for (i=0;i<10;++i)
            printf("%s\t%d\n", name[i], score[i]);
      }
      return 0;
}

Errors/Warnings:

a:\scores.c(10) : error C2085: 'i' : not in formal parameter list
a:\scores.c(10) : error C2143: syntax error : missing ';' before 'type'
a:\scores.c(10) : error C2085: 'name' : not in formal parameter list
a:\scores.c(11) : error C2085: 'score' : not in formal parameter list
a:\scores.c(13) : error C2065: 'i' : undeclared identifier
a:\scores.c(13) : error C2065: 'score' : undeclared identifier
a:\scores.c(13) : error C2109: subscript requires array or pointer type
a:\scores.c(13) : warning C4013: 'rand' undefined; assuming extern returning int
a:\scores.c(13) : error C2106: '=' : left operand must be l-value
a:\scores.c(14) : error C2065: 'name' : undeclared identifier
a:\scores.c(14) : error C2109: subscript requires array or pointer type
a:\scores.c(15) : error C2109: subscript requires array or pointer type
a:\scores.c(15) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
a:\scores.c(15) : warning C4024: 'strcpy' : different types for formal and actual parameter 1
a:\scores.c(16) : error C2109: subscript requires array or pointer type
a:\scores.c(16) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
a:\scores.c(16) : warning C4024: 'strcpy' : different types for formal and actual parameter 1
a:\scores.c(17) : error C2109: subscript requires array or pointer type
a:\scores.c(17) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
a:\scores.c(17) : warning C4024: 'strcpy' : different types for formal and actual parameter 1
a:\scores.c(18) : error C2109: subscript requires array or pointer type
a:\scores.c(18) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
a:\scores.c(18) : warning C4024: 'strcpy' : different types for formal and actual parameter 1
a:\scores.c(19) : error C2109: subscript requires array or pointer type
a:\scores.c(19) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
a:\scores.c(19) : warning C4024: 'strcpy' : different types for formal and actual parameter 1
a:\scores.c(20) : error C2109: subscript requires array or pointer type
a:\scores.c(20) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
a:\scores.c(20) : warning C4024: 'strcpy' : different types for formal and actual parameter 1
a:\scores.c(21) : error C2109: subscript requires array or pointer type
a:\scores.c(21) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
a:\scores.c(21) : warning C4024: 'strcpy' : different types for formal and actual parameter 1
a:\scores.c(22) : error C2109: subscript requires array or pointer type
a:\scores.c(22) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
a:\scores.c(22) : warning C4024: 'strcpy' : different types for formal and actual parameter 1
a:\scores.c(23) : error C2109: subscript requires array or pointer type
a:\scores.c(23) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
a:\scores.c(23) : warning C4024: 'strcpy' : different types for formal and actual parameter 1
a:\scores.c(24) : error C2109: subscript requires array or pointer type
a:\scores.c(24) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
a:\scores.c(24) : warning C4024: 'strcpy' : different types for formal and actual parameter 1
a:\scores.c(27) : error C2109: subscript requires array or pointer type
a:\scores.c(27) : error C2109: subscript requires array or pointer type
0
ozoCommented:
As the first error mesage says, 'i' is not in formal parameter list (void)
You probably wanted to make those declarations inside the function, (not in a declaration list for the parameters, of which there are none)
0
AlexVirochovskyCommented:
#include <stdio.h>
#include <string.h>
int main (void)
{
  int i
  char name [10] [15];
  int score [10];
 
 for (i=0; i < 10; score[i++]=rand());
 
 strcpy(name[0], "Mary Doe");
 strcpy(name[1], "Peter Chi");
 strcpy(name[2], "Dean Pet");
 strcpy(name[3], "John Lee");
 strcpy(name[4], "Sue Smith");
 strcpy(name[5], "Helen Di");
 strcpy(name[6], "Keith Pot");
 strcpy(name[7], "Sam Spade");
 strcpy(name[8], "Jane Run");
 strcpy(name[9], "Linda Brown");

 for (i=0;i<10;++i)
  printf("%s\t%d\n", name[i], score[i]);
 
return 0;
}
Alex
0
cbrookhartAuthor Commented:
The rand() function doesn't work, so I have not included that in my program. The compiler says that there is a syntax error with

char name[10] [15];

If I remove the ; from that line and the line below it I get two additional syntax errors.
0
ozoCommented:
Try adding a ; to the line above.
0
hongjunCommented:
You have to include the stdlib.h library in order to use rand function
0
AlexVirochovskyCommented:
of course must be
int i;
0
cbrookhartAuthor Commented:
I still am not able to get this program to work. At this point, all I want it to is to print the following

Firstname Lastname  96
Firstname Lastname  76

I have tried changing the order of the code, but every time I compile the code, I end up with the same errors and warnings.

int main (void)
{

    int i;
    char name [10] [15];
    int score [10];

      for (i=0; i < 10; score[i++]);
            printf("%s\n", name[0]);
      strcpy(name[0], "Mary Doe");
      strcpy(name[1], "Peter Chi");
      strcpy(name[2], "Dean Pet");
      strcpy(name[3], "John Lee");
      strcpy(name[4], "Sue Smith");
      strcpy(name[5], "Helen Di");
      strcpy(name[6], "Keith Pot");
      strcpy(name[7], "Sam Spade");
      strcpy(name[8], "Jane Run");
      strcpy(name[9], "Linda Brown");
      for (i=0;i<10;i++)
            printf("%s\t%d\n", name[i], score[i]);
      strcpy(score[0], "96");
      strcpy(score[1], "89");
      strcpy(score[2], "67");
      strcpy(score[3], "88");
      strcpy(score[4], "76");
      strcpy(score[5], "83");
      strcpy(score[6], "78");
      strcpy(score[7], "81");
      strcpy(score[8], "70");
      strcpy(score[9], "92");
      return 0;
}

Do I need to remove the second for loop and print statement in order for the program to run? The errors and warnings are the same as the ones in the previous message.
0
ozoCommented:
strcpy takes a char *, not an int.  you probably want
score[0] = 96;
not
strcpy(score[0], "96");
(unless you want to declare char score[10][15])
Also, you are printing name[0] and score[i] before you set them

0
cbrookhartAuthor Commented:
I got it to work, however the first name prints twice and the scores are being printed as 6683844. The other method of using score[0] = 96; didn't work.
0
ozoCommented:
What does your code look like now?
0
cbrookhartAuthor Commented:
int main (void)
{

    int i;
    char name [10][15];
    int score [10][15];
for (i=0; i < 10; score[i++]);
printf("%s\n", name[0]);
strcpy(name[0], "Mary Doe");
strcpy(name[1], "Peter Chi");
strcpy(name[2], "Dean Pet");
strcpy(name[3], "John Lee");
strcpy(name[4], "Sue Smith");
strcpy(name[5], "Helen Di");
strcpy(name[6], "Keith Pot");
strcpy(name[7], "Sam Spade");
strcpy(name[8], "Jane Run");
strcpy(name[9], "Linda Brown");
for (i=0; i < 10; score[i++]);
printf("%s\n", name[0]);
strcpy(score[0], "96");
strcpy(score[1], "89");
strcpy(score[2], "67");
strcpy(score[3], "88");
strcpy(score[4], "76");
strcpy(score[5], "83");
strcpy(score[6], "78");
strcpy(score[7], "81");
strcpy(score[8], "70");
strcpy(score[9], "92");
for (i=0;i<10;i++)
printf("%s\t%d\n", name[i], score[i]);
printf("\n\n");
      printf("Press<enter>to continue\n");
      while (getchar()!='n');
return 0;
}
0
ozoCommented:
for (i=0; i < 10; score[i++]); /*what is this supposed to do?*/

again, strcpy wants a char*, not an int or int*
and %d wants an int, not an int*
0
cbrookhartAuthor Commented:
The for loop is supposed to increment the name and score so that each name and score is printed out until the end of the list is reached. (Initialize-->i=0 Continue processing until reaching the last record--> i<10 Increment the counter so that each record is printed --> score[i++])

/* char*score[0]?*/
0
ozoCommented:
Do you know that the ; at the end terminates the
for (i=0; i < 10; score[i++]) ;
statement?  and that that context, score[i++] has exactly the same effect as i++ ?
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.