Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
Finding largest number in c program

The brief is to input 10 salaries, compute commission, and overall take home, and print highest takehome. I am able to get the highest number, but for the number inputted, and not for the takehome (which I calculate). I wonder how I do this as I'm not storing the take home values I'm calculating? Is this my issue? Please see code below.

#include <stdio.h>
 
int main()
{
    int num[10];
    int i = 0;
    int largest;
    int com, totaltakehome, finalsalary;
         
    while(i < 10)
    {
        printf("Enter salesperson salary for week[%d]:", i + 1);
        scanf("%d", &num[i]);
        com = num[i] / 100 * 7;
        printf("Commision is %d\n", com);
        totaltakehome = 300 + com;
        printf("Total takehome is %d\n", totaltakehome);
        printf("\n");
    }

    largest = num[0];
    i = 0;
    while(i < 10)
    {
        if(num[i] > largest)
        {
            largest = num[i];
        }
        i++;
    }

    printf("\nHighest salary is : %d\n", largest);

    return 0;
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Since none of the previously entered salaries are needed to calculate ...

  • the largest commission
  • the total takehome

... you can skip the num[10] array.

  • You do need to increase i in the loop. i+1 is an expression which does not change the value of i. I suggest using a for loop for this which keeps the declaration of i, the condition (i < 10) and the increment nicely in one place.
  • You should check the return value from scanf. If you scan for one thing, it should return 1, otherwise it failed.
  • Save the largest commission in the loop.
  • Initialize variables.
  • Add to totaltakehome. You currently assign a new value to it in every loop.

Example:

#include <stdio.h>

int main() {
    int largest = 0;       // initialized (and assuming this can't be negative)
    int totaltakehome = 0; // initialized

    for(int i = 0; i < 10; ++i) {  // loop with increment
        printf("Enter salesperson salary for week[%d]:", i + 1);

        int num;
        if(scanf("%d", &num) != 1) {   // check for success
            fprintf(stderr, "user error, aborting\n");
            return 1;
        }

        // an alternative commission forumla
        int com = num * 7 / 100; // perhaps a more fair commission

        printf("Commision is %d\n", com);

        // save this commission if it's the largest so far:
        if(com > largest) largest = com;

        // Add to `totaltakehome` - don't assign:
        totaltakehome += 300 + com;    // note: `+=`, not `=`

        printf("\n");
    }

    // print results after the loop:
    printf("Largest commision %d\n", largest);
    printf("Total takehome is %d\n", totaltakehome);
}
over 4 years ago · Santiago Trujillo Relatório

0

We can use a basic for loop for the answer.

int old = arr[0];
for (i = 0; i < 10; i++) {
    if (arr[i] > old) old = arr[i];
}

return old;

First we create a loop that runs ten times or we can also use

sizeof(arr)/sizeof(arr[0])

For an unknown list size. Forgive me for I am on mobile.

Then we check if the next number is bigger than our old biggest known if so set it to be the biggest. If so, we will set it to be our biggest. Then we finally return our value. (This is a function btw)

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda