how to find max number among results - c

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");
}

#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));
}

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;
}

Related

why my outer loop is not repeating itself?

#include<stdio.h>
void printarr( int arr , int a,int b){
for(int z=0;z<a;z++){
for(int x=0;x<b;x++){
printf("the marks of student %d in subject %d and %d is :%d\n",z+1 ,
x+1 , x+2 , arr);
}
}
}
int main(){
int n_students = 5;
int n_sub = 2;
int marks[5][2];
for (int i=0; i<n_students; i++){
for (int j=0; j<n_sub; j++){
printf("enter the marks of student %d in subject %d\n", i+1, j+1);
scanf("%d", marks[i][j]);
}
}
printarr(marks , 5 , 2);
return 0;
i am getting to put only two times and the outer loop is not repeating itself
please explain me in simple terms , i am just learning this launguage
complete begineer.
1.When passing two-dimensional arrays, it is not mandatory to specify the number of rows in the array. However, the number of columns should always be specified.
void printarr( int arr , int a,int b){ --->void printarr( int arr [][2], int
a,int b){
2.When reading array element in scanf you have missed &
scanf("%d", marks[i][j]);--->scanf("%d", &marks[i][j]);
3.When Printing array elements in printf you have to specify the index.
arr ---> arr[i][j]
#include<stdio.h>
void printarr( int arr [][2], int a,int b){
for(int z=0;z<a;z++){
for(int x=0;x<b;x++){
printf("the marks of student %d in subject %d is :%d\n",z+1,x+1,arr[z][x]);
}
}
}
int main(){
int n_students = 5;
int n_sub = 2;
int marks[5][2];
for (int i=0; i<n_students; i++){
for (int j=0; j<n_sub; j++){
printf("enter the marks of student %d in subject %d\n", i+1, j+1);
scanf("%d", &marks[i][j]);
}
}
printarr(marks, 5 , 2);
return 0;
}
You want this: (explanation in comments)
#include <stdio.h>
#include<stdio.h>
void printarr(int a, int b, int arr[a][b]) { // pass an array, not an int,
// and a and b must be before arr
for (int z = 0; z < a; z++) {
for (int x = 0; x < b; x++) {
printf("the marks of student %d in subject %d and %d is :%d\n", z + 1,
x + 1, x + 2, arr[z][x]); // use arr[x][z] here. arr obviously dons' make sense
}
}
}
int main() {
int n_students = 5;
int n_sub = 2;
int marks[5][2];
for (int i = 0; i < n_students; i++) {
for (int j = 0; j < n_sub; j++) {
printf("enter the marks of student %d in subject %d\n", i + 1, j + 1);
scanf("%d", &marks[i][j]); // you forgot the &
}
}
printarr(5, 2, marks); // you need to pass the size beforen the array
return 0; // read the documentation about VLAs
}

C.find an error in a very small program.Calculating the sum of the first k numbers of the sequence

Calculating the sum of the first k numbers of the sequence a[0] = 1, a[k] = k*a[k-1] +1/k ( k = 1, 2, ... ).
UPD
There is still a problem with the recursive function ...What is the error?
#include <stdio.h>
#include <stdlib.h>
float m(float n){
float k=1;
float sum=k;
int i;
for (i=1; i<n;i++){
k = (i*k+1.0/i);
sum = sum+k;
}
return sum;
}
float Fn(float n)
{
if (n==0) {
return 1;}
return ((n*Fn(n-1)+1.0/n)+Fn(n-1));
}
int main(int argc, char *argv[]) {
float k;
printf("input k : ");
scanf("%f",&k);
printf("res %f \n",Fn(k));
return 0;
}
There were several issues in your code:
Integer division: 1/n = 0
There was a confusion between the term value Fn and the sum value
An iterative solution is simpler here than a recursive one
Here is a code, with both iterative and recursive implementations:
#include <stdio.h>
#include <stdlib.h>
float sum_fn(int n){
float Fk = 1;
float sum = Fk;
for (int i = 1; i <= n; i++){
Fk = i*Fk + 1.0/i;
sum += Fk;
}
return sum;
}
float sum_recursive(int n, float *sum){
if (n == 0) {
*sum += 1.0;
return 1.0;
}
float Fn = n * sum_recursive(n-1, sum) + 1.0/n;
*sum += Fn;
return Fn;
}
int main(int argc, char *argv[]) {
int k;
printf("input k : ");
scanf("%d", &k);
printf("k = %d\tsum = %f\n", k, sum_fn(k));
float sum = 0;
sum_recursive(k, &sum);
printf("k = %d\tsum = %f\n", k, sum);
return 0;
}

temperature converter using functions in c programming

#include <stdio.h>
int converttemperature(int f,int c)
int main()
{
int farenheit, celcius, upper, lower, step;
upper=300; lower=0; step=20;
farenheit=lower;
while(farenheit<=upper)
{
converttemperature(farenheit, celcius);
printf("%d\t %d\n", farenheit, celcius);
farenheit = farenheit+step;
}
return 0;
}
int converttemperature(int f, int c)
{
int f,c;
c = 5 * (f-32) / 9;
return c;
}
#include <stdio.h>
int converttemperature(int f);
int main(void)
{
int farenheit, celcius, upper, lower, step;
upper=300; lower=0; step=20;
farenheit=lower;
while(farenheit<=upper)
{
celcius = converttemperature(farenheit);
printf("%d\t %d\n", farenheit, celcius);
farenheit = farenheit+step;
}
return 0;
}
int converttemperature(int f)
{
int c;
c = 5 * (f-32) / 9;
return c;
}

C - maximum value from one dimensional array's groups

So, my task is to let user fill one dimensional array A. This array has M number of groups and P members in each group. I need to fill array B with maximum values of each group in A array and display results of maximum values in each group after. I couldn't figure out any way to do it. I would really appreciate your help since I am a beginner programmer and doing my best to study. So my main problem in code is fillBArray function.
#include <stdio.h>
#include <stdlib.h>
int checkingvariable(int k, int a, int b);
void fillArray(int M, int P,int A[]);
void printArray(int M, int P,int A[]);
void fillBArray(int M, int P,int A[]);
int main()
{
int M, P;
printf("Enter M (the number of groups): ");
scanf("%d", &M);
M=checkingvariable(M, 1, 10);
printf("Enter P (the number of cars in one group): ");
scanf("%d", &P);
P=checkingvariable(P, 1, 10);
int i = P*M;
int A[i];
fillArray(M, P, A);
printArray(M, P, A);
fillBArray(M, P, A);
return 0;
}
void printArray(int M, int P,int A[])
{
int i;
for (i=0 ; i< M*P ; i++)
{
printf("%d ", A[i]);
printf("\n");
}
}
void fillArray(int M, int P, int A[])
{
int i;
for (i=0 ; i< M*P ; i++)
{
printf("Enter speed of car %d: ", i+1);
scanf("%d", &A[i]);
}
}
void fillBArray(int M, int P, int A[])
{
int c, k=0;
int maximum = A[0];
int B[M], group;
for (c = 0; c < P*M; c++)
{
if (A[c] > maximum)
{
maximum = A[c];
}
maximum = B[k];
printf("Maximum value for %d group is: %d", group, maximum);
}
}
int checkingvariable(int k, int a, int b)
{
if (k<a || k>b)
{
while(k<a || k>b)
{
printf("Enter correct value between %d and %d: ", a, b);
scanf("%d", &k);
}
}
return k;
}
One way is to replace one loop with two nested loops. The outer loop would iterate groups, while the nested loop would iterate members in each group.
One observation before you begin: members of a group g in the array A are located between indexes g*P, inclusive, and (g+1)*P, exclusive. Member m of a group is located at the index A[g*P + m].
Now it should be clear how to make your loops:
for (int g = 0 ; g != M ; g++) { // Groups
int max = A[g*P];
for (int m = 1 ; m != P ; m++) { // Members
... // Compute max
}
// Store max for group g in B[]
}
#include <stdio.h>
#include <stdlib.h>
int checkingvariable(int k, int a, int b);
void fillArray(int M, int P,int A[]);
void printArray(int M, int P,int A[]);
void fillBArray(int M, int P,int A[]);
int main()
{
int M, P;
printf("Enter M (the number of groups): ");
scanf("%d", &M);
M=checkingvariable(M, 1, 10);
printf("Enter P (the number of cars in one group): ");
scanf("%d", &P);
P=checkingvariable(P, 1, 10);
int i = P*M;
int A[i];
fillArray(M, P, A);
printArray(M, P, A);
fillBArray(M, P, A);
return 0;
}
void printArray(int M, int P,int A[])
{
int i;
for (i=0 ; i< M*P ; i++)
{
printf("%d ", A[i]);
printf("\n");
}
}
void fillArray(int M, int P, int A[])
{
int i;
for (i=0 ; i< M*P ; i++)
{
printf("Enter speed of car %d: ", i+1);
scanf("%d", &A[i]);
}
}
/*void fillBArray(int M, int P, int A[])
{
int c, k=0;
int maximum = A[0];
int B[M], group;
for (c = 0; c < P; c++)
{
if (A[c] > maximum)
{
maximum = A[c];
}
if (c < P)
{
group = 1;
maximum = A[c];
printf("Maximum value for %d group is: %d", group, maximum);
}
if (c < P*2)
{
group = 2;
k=1;
maximum = A[c];
printf("Maximum value for %d group is: %d", group, maximum);
}
}
}*/
void fillBArray(int M, int P, int A[])
{
int B[M], maximum = 0, m;
for (int g = 0 ; g != M ; g++)// Groups
{
int max = A[g*P];
for (m = 0 ; m != P ; m++) // Members
{
if (A[g*P + m] > maximum)
{
maximum = A[g*P + m];
}
}
B[g] = maximum;
printf("Maximum value for %d group is: %d", g+1, maximum);
printf("\n");
maximum = 0;
}
}
int checkingvariable(int k, int a, int b)
{
if (k<a || k>b)
{
while(k<a || k>b)
{
printf("Enter correct value between %d and %d: ", a, b);
scanf("%d", &k);
}
}
return k;
}
This is answer to my problem, check fillBArray.

Exercise in C to calculate sum from x to y

My teacher wants the sum of all numbers from x to y... like x+(x+1)+(x+2)...until y. But I think I'm doing something wrong here!
Can someone advice me what is wrong here?
#include <stdio.h>
int sum_naturals(int n)
{
return (n-1) * n / 2;
}
int sum_from_to(int m)
{
return (m-1) * m / 2;
}
void test_sum_naturals(void)
{
int x;
scanf("%d", &x);
int z = sum_naturals(x);
printf("%d\n", z);
}
void test_sum_from_to(void)
{
int x;
int y;
scanf("%d", &x);
scanf("%d", &y);
int z = sum_naturals(x);
int b = sum_from_to(y);
printf("%d\n", z);
}
int main(void)
{
//test_sum_naturals();
test_sum_from_to();
return 0;
}
Your code should in fact be:
int sum_naturals(int n)
{
return (n+1) * n / 2;
}
int sum_from_to(int m)
{
return (m+1) * m / 2;
}
Notice + instead of your -.
To find the sum just add in the function test_sum_from_to this line:
printf("The sum is %d", b-z);
Here's one solution :
#include<stdio.h>
int sum_naturals(int n)
{
return (n+1) * n / 2;
}
int sum_from_x_to_y(int x, int y){
return sum_naturals(y) - sum_naturals(x);
}
main()
{
printf ("Sum: %d \n",sum_from_x_to_y(5, 10));
printf ("Sum: %d \n",sum_from_x_to_y(0, 10));
printf ("Sum: %d \n",sum_from_x_to_y(0, 5));
return 0;
}
Note : sum from 0 to N is (n+1)*n/2 and not (n-1)*n/2

Resources