how to find the minimum element in an array using functions in c | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how to find the minimum element in an array using functions in c

10th May 2020, 3:56 AM
danny
danny - avatar
8 Answers
10th May 2020, 7:14 AM
Bensen
+ 1
#include <stdio.h> int sumofarray(int a[],int n) { int min,max,i; min=max=a[0];     for(i=1; i<n; i++)     {          if(min>a[i])   min=a[i];      if(max<a[i])     max=a[i];           }          printf("minimum of array is : %d",min);     printf("\nmaximum of array is : %d",max); } int main() {     int a[1000],i,n,sum;        printf("Enter size of the array : ");     scanf("%d", &n);     printf("Enter elements in array : ");     for(i=0; i<n; i++)     {         scanf("%d",&a[i]);     }     sumofarray(a,n); }
10th May 2020, 4:15 AM
VIVEK NIRMALKAR
VIVEK NIRMALKAR - avatar
0
Thanks but i wanted to pass the array to a function, that will find the smallest
10th May 2020, 4:08 AM
danny
danny - avatar
0
I really don't understand cause i did it a way and its giving me the wrong answer
10th May 2020, 5:33 AM
danny
danny - avatar
0
#include <stdio.h> #include <stdlib.h> #include <conio.h> int minimum(int proof_array[], int n ); int main(){ FILE *fp; int i, n, choice, integers[10], proof_array[10]; int  minimum; char ch; while (1) { //part A printf("**************************************\n"); printf("           PSIL's Project             \n"); printf("**************************************\n"); printf("     1. Work with an arry\n"); printf("     2. Work with text file\n"); printf("     3. Work with binary file\n"); printf("     4. View all users in text file\n"); printf("     5. View all user in binary file\n"); printf("     6. Exit the program\n"); printf("**************************************\n"); printf("  Please select an option to continue: \n"); printf("**************************************\n"); scanf("%d",&choice); system("cls"); switch(choice) { //part b case 1:    fp=fopen("array_values.txt", "w");     if(fp==NULL)     {     printf("error: cannot open file sorry");     exit(1); } printf("\n enter 10 integer values: ");     for
10th May 2020, 5:36 AM
danny
danny - avatar
0
case 1:    fp=fopen("array_values.txt", "w");     if(fp==NULL)     {     printf("error: cannot open file sorry");     exit(1); } printf("\n enter 10 integer values: ");     for(i=0;i<10;i++) { scanf("%d",&integers[i]); } for(i=0;i<10;i++){ fprintf(fp,"%d\n",integers[i]); }     fclose(fp);     //reread    fp=fopen("array_values.txt", "r");     if(fp==NULL)     {     printf("error: cannot open file sorry");     exit(1); } printf("The contents of file are:\n", integers);    while((ch = fgetc(fp)) != EOF)       printf("%c", ch);    fclose(fp); // store to another array if (fp = fopen("array_values.txt", "r")) {         while (fscanf(fp, "%d", &proof_array[i]) != EOF) {             ++i;         }         fclose(fp);     }     //for (--i; i >= 10; --i)        // printf("proof_array[%d] = %d\n", i, proof_array[i]);                    printf("\nSmallest Element : %d", minimum);   //function 1    int minimum(int proof_array[], int n ){     int i=0;     int min = proof_array[0];     for(i=0;i<n;i++){     if(proof_ar
10th May 2020, 5:37 AM
danny
danny - avatar
0
Its one program it was just too long to send , so i sent it in 2
10th May 2020, 5:38 AM
danny
danny - avatar