mousou
mousou

Reputation: 13

how to find max number among results

I am given 5 integers that must be not less than 10 and not more than 100. Then, I must find their division remainders if we divide them by 10. Then, I must find the sum of the remainders that I found and (there it goes the difficult part) finally which one of the 5 remainders is the max.

Here, I give you the code that I wrote but I have no idea how to go further. Maybe with "for", but I don't imagine how exactly.

#include <stdio.h>
#include <stdlib.h>
int main()
{
  int a, b, c, d, e, a10,b10,c10,d10,e10, sum, max;
  printf("give 5 integers \n");
  scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
  a10 = a % 10;
  b10 = b % 10;
  c10 = c % 10;
  d10 = d % 10;
  e10 = e % 10;
  printf("division remainder is: %d %d %d %d %d\n", 
  a10,b10,c10,d10,e10);
  sum = a10 + b10 + c10 + d10 + e10;
  printf("the sum of the remains is: %d\n", sum);

  system("pause");
}

Upvotes: 1

Views: 113

Answers (2)

BLUEPIXY
BLUEPIXY

Reputation: 40145

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>

int *rands_range(int n, int low, int high);
void print(FILE *fp, int n, int *nums);
int *map(int n, int *nums, int (*f)(int));
int fold(int n, int *nums, int init, int (*f)(int, int));
int add(int x, int y);
int rem10(int x);
int greater(int x, int y);

int main(void){
    srand(time(NULL));
    int n = 5;

    printf("give 5 integers\n");
    int *integers = rands_range(n, 10, 100);
    print(stdout, n, integers);
    putchar('\n');

    printf("division remainder is: ");
    int *rems = map(n, integers, rem10);
    print(stdout, n, rems);
    putchar('\n');

    int sum = fold(n, rems, 0, add);
    printf("the sum of the remains is: %d\n", sum);
    putchar('\n');

    int max = fold(n, rems, *rems, greater);
    printf("the max of the remains is:  %d\n", max);
    putchar('\n');

    free(integers);
    free(rems);
    system("pause");
}

int *rands_range(int n, int low, int high){
    assert(n > 0 && RAND_MAX >= high - low && high >= low);

    int *values = malloc(n * sizeof(*values));
    if(values){
        for(int i = 0; i < n; ++i){
            values[i] = low + rand() % (high-low+1);
        }
    }
    return values;
}
void print(FILE *fp, int n, int *nums){
    for(int i = 0; i < n; ++i){
        if(i)
            fprintf(fp, ", ");
        fprintf(fp, "%d", nums[i]);
    }
    fprintf(fp, "\n");
}
int remainder_int(int x, int y){
    return x % y;
}
int add(int x, int y){
    return x + y;
}
int rem10(int x){
    return remainder_int(x, 10);
}
int greater(int x, int y){
    return x > y ? x : y;
}
int *map(int n, int *nums, int (*f)(int)){
    int *result = malloc(n * sizeof(*result));
    if(result){
        for(int i = 0; i < n; ++i){
            result[i] = f(nums[i]);
        }
    }
    return result;
}
int fold(int n, int *nums, int init, int (*f)(int, int)){
    int acc = init;
    for(int i = 0; i < n; ++i){
        acc = f(acc, nums[i]);
    }
    return acc;
}

Functions with variable arguments:

#include <stdio.h>
#include <stdarg.h>
#include <limits.h>

int max(int n, ...){
    int ret = INT_MIN;
    va_list ap;

    va_start(ap, n);
    while(n--){
        int wk = va_arg(ap, int);
        if(ret < wk)
            ret = wk;
    }
    va_end(ap);

    return ret;
}

int sum(int n, ...){
    int sum = 0;
    va_list ap;

    va_start(ap, n);
    while(n--){
        sum += va_arg(ap, int);
    }
    va_end(ap);

    return sum;
}

int main(void){
    int a = 6, b = 3, c = 9, d = 2, e = 8;
    printf("sum:%d\n", sum(5, a, b, c, d, e));
    printf("max:%d\n", max(5, a, b, c, d, e));
}

Upvotes: 1

cse
cse

Reputation: 4104

Following is the code using 'for' and int array. To use 'for' you must need to use array.

#include<stdio.h>
#include <stdlib.h>

int main()
{
    int i;
    int max;
    int sum=0;
    int aryNum[5];
    int aryRem[5];

    printf("give 5 integers \n");
    for(i=0;i<5;i++)
    {
        scanf("%d",&aryNum[i]);
    }

    printf("division remainder is: ");
    for(i=0;i<5;i++)
    {
        aryRem[i]=aryNum[i]%10;
        printf("%d ",aryRem[i]);
    }
    printf("\n");

    max=aryRem[0];
    sum = max;
    for(i=1;i<5;i++)
    {
        if(max<aryRem[i])
        {
            max=aryRem[i];
        }
        sum+=aryRem[i];
    }
    printf("the sum of the remains is: %d\n", sum);
    printf("maximum remains is: %d\n", max);
    system("pause");
    return 0;
}

If you don't want to use 'for' and int array then you can use following code. But it is not advisable:

#include <stdio.h>
#include <stdlib.h>

int greater(int a, int b)
{
    return (a>b)? a:b;
}

int main()
{
  int a, b, c, d, e, a10,b10,c10,d10,e10, sum, max;
  printf("give 5 integers \n");
  scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
  a10 = a % 10;
  b10 = b % 10;
  c10 = c % 10;
  d10 = d % 10;
  e10 = e % 10;
  printf("division remainder is: %d %d %d %d %d\n",
  a10,b10,c10,d10,e10);
  sum = a10 + b10 + c10 + d10 + e10;
  printf("the sum of the remains is: %d\n", sum);

  /*New added code*/
  max=greater(a10,b10);
  max=greater(max,c10);
  max=greater(max,d10);
  max=greater(max,e10);
  printf("maximum remains is: %d\n", max);

  system("pause");
  return 0;
}

Upvotes: 0

Related Questions