Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For the following problem, you must use pointer to replace the array name. Defin

ID: 3681031 • Letter: F

Question

For the following problem, you must use pointer to replace the array name. Define the following functions:

a) A function that sets an array of int with random numbers

b) A function that outputs an array of int.

c) A function that computes the average of an array of int

d) A function that finds the smallest element and its index of an array of int

e) A function that search for a particular value in an array of int.

Write a C program that generate 30 random numbers of integers and store them in an array of int. Use the above functions you create to find:

a) The average

b) The smallest element and its index. (you must print the two results from the main function)

c) Search for an existing number as well as non-existing number

Explanation / Answer

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

void setArray(int *arr, int n);
void display(int *arr, int n);
float average(int *arr, int n);
int smallest(int *arr, int *smallest, int n);
int search(int *arr, int n, int key);

int main(){
  
   int N, *arr;
   printf("Enter size of array: ");
   scanf("%d", &N);
   // creating an array of size N - dynamically
   arr = (int *)malloc(N*sizeof(int));
  
   // setting integers into array
  
   setArray(arr, N);
  
   //computing average
   float avg = average(arr, N);
   printf("Average: %.2f ",avg);
  
   // finding smallest element
   int small = 0, index;
   index = smallest(arr, &small, N);
   printf("%d is smallest element present at index %d ",small, index);
  
   // searching an array:
   int key;
   printf("Enter number to search: ");
   scanf("%d", &key);
   index = search(arr, N, key);
   if(index == -1)
       printf("%d is not in array ",key);
   else
       printf("%d is present at index %d in array ",key, index);
   return 0;
}

void setArray(int *arr, int n){
  
       int i=0;
       for(i=0; i<n; i++){
               arr[i] = rand()%100 + 1; // generating random number in range 1-100
                                       // you can change accordig to youe need
      
       }
}

void display(int *arr, int n){
       int i;
       for(i=0; i<n; i++)
           printf("%d ",arr[i]);
       printf(" ");
}
// this function set smallest element in 'smallest' and returns its index
int smallest(int *arr, int *smallest, int n){
  
       // setting smallest elements to first element of an array
       *smallest = arr[0];
       int index = 0;
       int i;
       for(i=1; i<n; i++){
               if(arr[i] < *smallest){
                       *smallest = arr[i];
                       index = i;
               }
       }
   return index;
}
// this function returns index of element if it is in array
// otherwise it returns -1 (not present)
int search(int *arr, int n, int key){
  
       int index = -1;
       int i;
       for(i=0; i<n; i++){
               if(arr[i] == key){
                       index = i;
                       break;
               }
       }
   return index;
}

float average(int *arr, int n){
       int i, sum =0;
       for(i=0; i<n; i++){
               sum = sum + arr[i];
       }
      
       return (float)sum/n;
}

/*

Output:

Enter size of array: 30
Average: 54.73
12 is smallest element present at index 20
Enter number to search: 12
12 is present at index 20 in array

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote