0

I am trying to find the lowest value in an integer array. Then displaying that value with the name of the person who has that score. I can find which value is the lowest adn display it with the index like player 1 or player 2 , but i cant put the name instead of that index.

#include <string.h>
#include <stdio.h>
#include <string.h>
#define LEN_NAME 34
#define NUM_NAMES 3
void lowest(int array[], char *fullName, int elements);
int main (void) {
    int scores[NUM_NAMES] = { 230,330,423};
    char firstName[NUM_NAMES][LEN_NAME] = {"john","james","mario"};
    char lastName[NUM_NAMES][LEN_NAME] = {"goirgi", "edison", "luca"};
    char *fullName[NUM_NAMES][LEN_NAME];
    int i;
    for (i=0; i < NUM_NAMES; i++) {
        strcpy(fullName[i], firstName[i]);
        strcat(fullName[i], " " );
        strcat(fullName[i], lastName[i]);
        printf("Your scores is %d with your full name is %s.\n",scores[i], fullName[i]);
    }

    lowest(scores,*fullName, NUM_NAMES);
    return 0;
}

void lowest (int array[], char *fullName, int elements) {
    int i,small = array[0], j;
    for (i=0; i< elements; i++) {
        if (array[i] < small) {
            small = array[i];
            j = i;
        }
    }
    printf("The lowest scored %d with score %d.\n", j , small);
}
1
  • 1
    did you try fullName[j]? Commented Jun 25, 2013 at 18:31

3 Answers 3

1

firstName, lastName and fullName are contiguous areas of memory that get treated as (LEN_NAME x NUM_NAMES) matrices. When you pass them arround the called function needs to know the row length (LEN_NAME) so that when it subscripts by i (fullName[i]) it will do the calculation fullName + (i * LEN_NAME) (here fullName is the address of the start of the memory area) so that it will get to the beginning of the i'th name.

#include <string.h>
#include <stdio.h>
#include <string.h>
#define LEN_NAME 34
#define NUM_NAMES 3
void lowest(int array[], char fullName[][LEN_NAME], int elements);
int main(void)
{
    int scores[NUM_NAMES] = { 230, 330, 423 };
    char firstName[NUM_NAMES][LEN_NAME] = { "john", "james", "mario" };
    char lastName[NUM_NAMES][LEN_NAME] = { "goirgi", "edison", "luca" };
    char fullName[NUM_NAMES][LEN_NAME];
    int i;
    for (i = 0; i < NUM_NAMES; i++) {
        strcpy(fullName[i], firstName[i]);
        strcat(fullName[i], " ");
        strcat(fullName[i], lastName[i]);
        printf("Your scores is %d with your full name is %s.\n", scores[i],
               fullName[i]);
    }

    lowest(scores, fullName, NUM_NAMES);
    return 0;
}

void lowest(int array[], char fullName[][LEN_NAME], int elements)
{
    int i, small = array[0], j = 0;
    for (i = 0; i < elements; i++) {
        if (array[i] < small) {
            small = array[i];
            j = i;
        }
    }
    printf("%s scored %d.\n", fullName[j], small);
}

It's usually more idiomatic to make arrays of pointers to chars for this situation:

char *fullName[NUM_NAMES];
fullName[0] = malloc(LEN_NAME);
// ...

You can either remember their length or put a NULL pointer in the last position. If you do this you'll need to declare lowest as:

void lowest(int array[], char *fullName[], int elements);
Sign up to request clarification or add additional context in comments.

Comments

0

An easy solution would be to pass an array of numbers and an array of names, and make sure that the lists have matching indices. Once you find the index of the lowest value you can then simply index into the list of names to display that one.

Comments

0

I think this is a typo:

char *fullName[NUM_NAMES][LEN_NAME];
     ^

Here you've declared a two-dimensional array of pointers, but you haven't pointed them to anything.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.