Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!