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

Assignment: Write a program that uses an array to store a series of integers. Th

ID: 3832569 • Letter: A

Question

Assignment: Write a program that uses an array to store a series of integers. The program should prompt the user to enter elements into the array. After the array has been filled, it should display the initial contents of the array. It should then perform operations to manipulate the elements of the array through specific functions. Note that if the array is modified in any way, the program should re-display the contents of the array.

Procedure:

Declare an array of integers of some pre-defined maximum size. You are free to decide the size of the array, but all the elements of the array should be initialized to zero.

Implement the following operations on the array through the specified functions:

input()
This function prompts the user to populate the array with data entered from the keyboard. As this is an array of integers, the data should all be numeric integers.

display()
This function outputs the contents of the array one element at a time. The array should be output using the following format:

Note: position, represents the logical index of the array and value represents the numeric integer stored in the corresponding physical location. In other words position 1 corresponds to the value at array[0].

statistics()
This function computes the minimum, maximum, sum, and average of all the elements in the array. Note that this function simply computes the information, it does not display it, that should happen in the body of the main function.

count()
This function accepts a number and determines how many times, if at all, the number appears as an element of the array. The function should return this value.

search()
This function searches the array for a specific element and returns the position in the array of the first instance of that element. If the specified element does not appear in the array, the function should return a -1.

replace()
This function replaces all instances of a specific element with a new value. The function should receive the value to replace and the new value to replace it with. The function should return the number of times it replaced the value.

modify()
This function modifies the element in a specific position of the array. It should receive as arguments both the postion of the array (i.e. 1 to n) to modify and the new value to assign to that position. Note that the position must be within 1 and the maximum size of the array but the position modifed is the corresponding physical location (i.e. array[0] to array[n-1]).The function should return a boolean variable of true or false respectively indicating whether the modification took place. In other words if the position is not within the bounds of the array, the function should return false.

clear()
This function either clears a single element in the array or every element of the array. The function should determine which to do based on the argument passed to it. If a -1 is passed, the function should clear the entire array, otherwise, it should clear the element at the specified position, assuming off course that the specified position is within the bounds of the array. This function should return a boolean variable of true or false respectively indication whether or not it was able to accomplish its' task.

Write the code to control the execution of our program and to test each operation of the array within the body of the main() function.

Explanation / Answer

NOTE: As per Chegg policy it is expected to answer first 4 questions. But i have answered as much as possible and is able to complete 6 questions(functions) in your assignment problem. The last 2 functions i was not able to complete because of lack of time.

Code:
#include <stdio.h>

/*
script: test_func.c
Script to perform various operations
*/

/* function declaration */
int input(int[]);
void display(int[], int);
void cal_statistics(int[], int);
int count_element(int[], int, int);
int search_element(int[], int, int);
int replace(int[], int, int, int);

int main()
{
   int arr[1000];
   int choice = 0;
   int n = 0;
   int x, y, frequency;
   int pos;
   int num_rep;

   /* Initializing all elements of array to zero */
   for(int i=0;i<1000;i++)
       arr[i]=0;
   while (choice != -1){  
       printf(" 1. Input a set of integers");
       printf(" 2. Display the contents of array");
       printf(" 3. Statistics of the elements stored");
       printf(" 4. Count an element in array");
       printf(" 5. Search an element in array");
       printf(" 6. Replace an element in array");
       printf(" 7. Modify an element in array");
       printf(" 8. Clear elements in array");
       printf(" Choose one of the options from 1..8 (-1 to quit) ");
       scanf("%d", &choice);
      
       if(choice == 1){
           n = input(arr);
       }
       else if(choice == 2){
           if (!(n > 0)){
               printf(" There are no elements stored in array yet. Please try option 1 to input elements ");
               continue;
           }
           display(arr, n);
       }
       else if(choice == 3){
           if (!(n > 0)){
               printf(" There are no elements stored in array yet. Please try option 1 to input elements ");
               continue;
           }
           cal_statistics(arr, n);
       }
       else if(choice == 4){
           if (!(n > 0)){
               printf(" There are no elements stored in array yet. Please try option 1 to input elements ");
               continue;
           }
           printf(" Enter the element to count occurrences in array: ");
           scanf("%d", &x);
           frequency = count_element(arr, n, x);
           printf(" The element %d occurred in array %d times", x, frequency);
       }
       else if(choice == 5){
           if (!(n > 0)){
               printf(" There are no elements stored in array yet. Please try option 1 to input elements ");
               continue;
           }
           printf(" Enter the element to search: ");
           scanf("%d", &x);  
           pos = search_element(arr, n, x);
           if (pos == -1){
               printf(" Element %d not found in array", x);
               continue;
           }
           printf(" Element %d found in the array at position %d", x, pos);
       }
       else if(choice == 6){
           if (!(n > 0)){
               printf(" There are no elements stored in array yet. Please try option 1 to input elements ");
               continue;
           }
           printf(" Enter the element to search: ");
           scanf("%d", &x);
           printf(" Enter the element to replace: ");
           scanf("%d", &y);
           num_rep = replace(arr, n, x, y);
           if(num_rep == -1){
               printf(" Element %d not found in array to replace", x);
               continue;
           }
           printf(" Element %d replaced with %d %d number of times", x, y, num_rep);
           display(arr, n);
       }
       else if(choice == 7){
       }
       else if(choice == 8){
       }
   }
   return 0;
}

/* function to take input from user and store in array */
int input(int arr[]){  
   int n;

   printf(" Enter how many elements you want to read: ");
   scanf("%d", &n);
   printf(" Enter the elements ");
   for(int i = 0; i < n; i++){
       scanf("%d", &arr[i]);
   }
   return n;
}

/* function to display elements in array */
void display(int arr[], int n){
   printf(" Position Value");
   for(int i = 0; i < n; i++)
       printf(" %8d %5d", i+1, arr[i]);
}

/* function to calculate max, min, sum and average */
void cal_statistics(int arr[], int n){
   int max, min, sum;
   float avg;

   sum = avg =0;
   max = arr[0];
   min = arr[0];
   for(int i = 0; i < n; i++){
       if(arr[i] > max)
           max = arr[i];
       if(arr[i] < min)
           min = arr[i];
       sum += arr[i];
   }
   avg = sum/n;
  
   printf(" Maximum value in array is: %d", max);
   printf(" Minimum value in array is: %d", min);
   printf(" Sum of the elements in array is: %d", sum);
   printf(" Average of elements of the array: %.2f", avg);
}

/* function to count number of occurrences of a given element */
int count_element(int arr[], int n, int x){
   int freq = 0;

   for(int i = 0; i < n; i++){
       if(x == arr[i])
           freq++;      
   }

   return freq;
}

/* function to search an element in array and return its first occurence */
int search_element(int arr[], int n, int x){
   int i;

   for(i = 0; i < n; i++){
       if(arr[i] == x)
           break;
   }

   if(i == n)
       return -1;
   else
       return i+1;
}

/* function to replace an element in array with given element and return occurences */
int replace(int arr[], int n, int x, int y){
   int replacements = 0;

   for(int i = 0; i < n; i++){
       if(arr[i] == x){
           arr[i] = y;
           ++replacements;
       }
   }

   if(replacements == 0)
       return -1;
   else   
       return replacements;
}

Execution and output:

186590cb0725:C bonkv$ gcc test_func.c
186590cb0725:C bonkv$ ./a.out

1. Input a set of integers
2. Display the contents of array
3. Statistics of the elements stored
4. Count an element in array
5. Search an element in array
6. Replace an element in array
7. Modify an element in array
8. Clear elements in array
Choose one of the options from 1..8 (-1 to quit) 1

Enter how many elements you want to read: 10

Enter the elements
20
90
20
19
8
100
1
-1
100
100

1. Input a set of integers
2. Display the contents of array
3. Statistics of the elements stored
4. Count an element in array
5. Search an element in array
6. Replace an element in array
7. Modify an element in array
8. Clear elements in array
Choose one of the options from 1..8 (-1 to quit) 2

Position   Value
1   20
2   90
3   20
4   19
5   8
6   100
7   1
8   -1
9   100
10   100
1. Input a set of integers
2. Display the contents of array
3. Statistics of the elements stored
4. Count an element in array
5. Search an element in array
6. Replace an element in array
7. Modify an element in array
8. Clear elements in array
Choose one of the options from 1..8 (-1 to quit) 3

Maximum value in array is: 100
Minimum value in array is: -1
Sum of the elements in array is: 457
Average of elements of the array: 45.00
1. Input a set of integers
2. Display the contents of array
3. Statistics of the elements stored
4. Count an element in array
5. Search an element in array
6. Replace an element in array
7. Modify an element in array
8. Clear elements in array
Choose one of the options from 1..8 (-1 to quit) 4

Enter the element to count occurrences in array: 200

The element 200 occurred in array 0 times
1. Input a set of integers
2. Display the contents of array
3. Statistics of the elements stored
4. Count an element in array
5. Search an element in array
6. Replace an element in array
7. Modify an element in array
8. Clear elements in array
Choose one of the options from 1..8 (-1 to quit) 4

Enter the element to count occurrences in array: 100

The element 100 occurred in array 3 times
1. Input a set of integers
2. Display the contents of array
3. Statistics of the elements stored
4. Count an element in array
5. Search an element in array
6. Replace an element in array
7. Modify an element in array
8. Clear elements in array
Choose one of the options from 1..8 (-1 to quit) 5

Enter the element to search: 100

Element 100 found in the array at position 6
1. Input a set of integers
2. Display the contents of array
3. Statistics of the elements stored
4. Count an element in array
5. Search an element in array
6. Replace an element in array
7. Modify an element in array
8. Clear elements in array
Choose one of the options from 1..8 (-1 to quit) 6

Enter the element to search: 100

Enter the element to replace: 1000

Element 100 replaced with 1000 3 number of times
Position   Value
1   20
2   90
3   20
4   19
5   8
6   1000
7   1
8   -1
9   1000
10   1000
1. Input a set of integers
2. Display the contents of array
3. Statistics of the elements stored
4. Count an element in array
5. Search an element in array
6. Replace an element in array
7. Modify an element in array
8. Clear elements in array
Choose one of the options from 1..8 (-1 to quit) -1
186590cb0725:C bonkv$

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