简体   繁体   中英

Use function to get value, find max, find min and display in C language

These are actually two programs but we needed to combine it on a single program with all 4 functions. The first function has to get value from two arrays and add them and store in another array. The second function has to find the largest element in the new array. The third function has to find the smallest element in new array. And finally, the fourth function has to display all the values from the above three functions ie The elements of third array which is sum of two arrays, then the maximum element of the sum array and the minimum value of the sum array. The data items have to be external storage class. No compile error on Borland C but the values are null here.

It requires concept of extern storage class, pointers with arrays and functions.

#include<stdio.h>
#include<conio.h>

int a[10], b[10], *c[10], i, *min, *max;

void main()
{

    //Function Prototypes
    void getvalue(void);
    void findmax(void);
    void findmin(void);
    void display(void);

    getvalue();
    findmax();
    findmin();
    display();

    getch();

}

void getvalue()
{
    printf("\n Enter 10 values for Array A: ");
    for(i=0;i<10;i++)
    {
        scanf("%d",a[i]);
    }

    printf("\n Enter 10 values for Array B: ");
    for(i=0;i<10;i++)
    {
        scanf("%d",b[i]);
    }

    for (i = 0; i < 10; i++)
    {
        c[i]=a[i]+b[i];
    }

}

void findmax()
{
    for (i = 0; i < 10; i++)
    {
        if(*(c+0)<=*(c+i))
        {
            *(c+0) = *(c+i);
        }
    }

    max = *(c+0);

}

void findmin()
{
    for(i=0;i<10;i++)
    {
        if(*(c+0)>=*(c+i))
        {
               *(c+0) = *(c+i);
        }
    }

    min = *(c+0);

}

void display()
{
    for (i = 0; i < 10; i++)
    {
        printf("\n\n The value of %d element of C = %d ",i,*(c+i) );
    }

    printf("\n\n The max value in C is %d",max);

    printf("\n\n The min value in C is %d",min);
}

well, you are modifying your array with find min and find max, which you shouldn't do.

and min and max are being stored incorrectly, as they are pointers. Don't dereference them at the end. Instead update them in the if statement in your loop, and compare to them.

If you are going to use pointers for an array, REALLY use pointers.

try looking at a loop that walks an array of 10 int's a, like: int * walker, *max;

for (max = a + 9, walker = max - 1; walker >= a; walker--)
{
     if (*max < *walker){
         max = walker;
     }
}

then max will be a pointer to the highest element.

int a[10], b[10], *c[10], i, *min, *max;
                      ^        ^     ^
                      |        |     |

It means your new new array is an array of pointers of integer type and you are taking the min and max variables as pointers.There is no need of doing this.Make it Simply.

int c[10],max,min;

Next,

 scanf("%d",a[i]);
             ^
             |

a[i] is representing the value of array at index i but scanf takes address in its input argument.so ypu have to make it this way.

scanf("%d",&a[i]);

Same in case of b[i].

Here are several problems here:

  1. You int *c[10] - means have the same sense as int **c, remove * from it, the same for *min, *max
  2. min/max functions: *(c+0) = *(c+i); - you are overwriting value in c[0], change it to max, like:

    max = c; for (i=1;i < 10;i++) if ( (c+i) > max) max = *(c+i);

  3. there is no extend storage, i suppose you need to move declarations of a,b,c arrays into separate file, in main file just notify that these variables are stored in another file with extern modifier

#include <stdio.h>
#include <math.h>
{
int R1=100;                     
int add(int R1, int R2,int RL)
int product (int I,int RL)                      
int main(void);

double R2, RL, V, I             
int W, Rtot
do while (R1>R2&&R2<RL{
        for(R2=0,R2<=10000,R2++)    
    { 
        printf ("Resistance for R2=%5.3f\n",R2);
}
    for(RL=100,RL<=15000,RL++)
    {   
        printf ("Resistance for loads resistance=%5.3f\n",RL);
    }
    for(V=1,V<=15,V+=.1)                    
{
    printf ("Input voltage is %3.1f",V);    
}
}
    Rtot=R1+R2+RL                           
        printf ("The total resistance is     %d+%f+%f=%d\n",R1,R2,RL,Rtot);
return (0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM