Question

Consider the following code segment: int main() cons int SIZE -20; int values [SIZE] - f0, 23, 34, -7, 110, 42,-350, 424, 25, 99, 10, 05, 50, -5, 1, 200, -350, 437, 25, 147}; // your code goes here Write a complete program to display the values from each of the following four steps All displays must be done in main. Use separate functions for each step below, passing the array to each function. 1.1) Provide the sum of the numbers in this array - function must return an int. 1.2) Provide the average of the positive numbers in this array - function must return a double. 1.3)Provide the lowest number in the array -function must return an int. 1.4) Provide the highest number in the array - function must return an int.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

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

double avg(int n , int *arr){
    double ans = 0;
    for(int i=0;i<n;i++){
        ans = ans+arr[i];
    }
    return ans/n;
}

int minimum(int n , int *arr){
    int ans = arr[0];
    for(int i=1;i<n;i++){
        if(arr[i]<ans){
            ans = arr[i];
        }
    }
    return ans;
}

int maximum(int n , int *arr){
    int ans = arr[0];
    for(int i=1;i<n;i++){
        if(arr[i]>ans){
            ans = arr[i];
        }
    }
    return ans;
}

int main(){
    const int SIZE = 20;
    int values[SIZE] = {0,23,34,-7,110,42,-350,424,25,99,10,05,50,-5,1,200,-350,437,25,147};
    printf("Sum = %d\n",sum(SIZE,values));
    printf("Avg = %lf\n",avg(SIZE,values));
    printf("min = %d\n",minimum(SIZE,values));
    printf("max = %d\n",maximum(SIZE,values));
    printf("");
}


OUTPUT :

D:\Coding CODE BLOCKSISampleCpp\bin\DebuglSampleCpp.exe Sum 920 Avg 46.000000 min-350 max437 Process returned e (exe) execut

Add a comment
Know the answer?
Add Answer to:
Consider the following code segment: int main() cons int SIZE -20; int values [SIZE] - f0,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size,...

    sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size, c;     float median;     printf("Enter the array size:\n");     scanf("%d", &size);     array = (int*) malloc(size * sizeof(int));     printf("Enter %d integers:\n", size);     for (c = 0; c < size; c++)         scanf("%d", &array[c]);     sort(array, size);     printf("Array sorted in ascending order:\n");     for (c = 0; c < size; c++)         printf("%d ", array[c]);     printf("\n");     median = find_median(array,...

  • #include <iostream> using namespace std; int * newZeroArray(int size) { //your code here } int main()...

    #include <iostream> using namespace std; int * newZeroArray(int size) { //your code here } int main() { int size = 10; int * A = newZeroArray(size); for(int i = 0; i < size; i++) cout << A[i] << " "; cout << endl; }Write a function that takes a size, creates a new array of that size with all zeros, and returns the array. If the size is not a valid size for an array, do not return a valid...

  • 20) What is the output of the following segment of C code: int avg(int n, int*...

    20) What is the output of the following segment of C code: int avg(int n, int* a); int main () {             int array[4]={1,0,6,9};             printf("%d", avg(4, array)+ 1);             system("pause");             return 0; } int avg(int n, int* a) { int i, sum=0; for (i=0;i<n;i++) { sum+=a[i]; } return sum/n; } a) 16 b) 5 c) 4 d) 8 21) What is the output of the following segment of C code: int x = 2; int y = 3;...

  • 31. The following code segment is syntactically correct:              int number{20};          cout << number <<...

    31. The following code segment is syntactically correct:              int number{20};          cout << number << setbase(16) << " " << number                               << setbase(10) << " " << number                                 << showpos << " " << number << endl;                 T__   F__       32. The following statement wants to determine if ‘count’ is outside the     range of 0 through 100:             if (count < 0 && count > 100)                                             T__   F__ 33. There is...

  • Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search and return...

    Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search and return } Selection_Sort(int a[], int z) { …..// do the selection sort } main() {     Selection_Sort(array, size);     Binary_Search(array, item); } Calculate the time complexity of this code and use O() notation.

  • howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE]...

    howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE] = {10, 10, 14, 16, 6, 25, 5, 8};    int index;    index=0;    res = values[index];    for (int j=1; j<SIZE; j++)    {        if (values[j] > res)        {            res = values[j];            index = j;        cout << index << res << endl;        }    }    cout <<...

  • Fix this C++ code so that the function prototypes come before main. Main needs to be...

    Fix this C++ code so that the function prototypes come before main. Main needs to be declared first. Then call to the functions from inside of main. #include<cstdlib> using namespace std; //prompt user for input void getGrades(int gradeArray[],int size) { for(int i=0; i<size; i++) { cout<<"Enter the grade "<<i+1<<": "; cin>>gradeArray[i]; } } // finding average of all numbers in array float computeAverage(int numbers[],int size) { float sum=0; for(int i=0; i<size; i++) { sum = sum + numbers[i]; //compute sum...

  • 1. Write code for a function that receives two parameters (a,and b) by value and two...

    1. Write code for a function that receives two parameters (a,and b) by value and two more parameters (c and d) by reference. All parameters are double. The function works by assigning c to (a/b) and assigning d to (a*b). The function has no return value. From main, use scanf to get two numbers, then call the function, and then display both outcome values to the output in a printf statement. 2. After part 1 is completed, write code to...

  • One dimensional array What this code print #include <iostream> using namespace std; int main () {...

    One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << “Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << “ “; cout << endl; return 0; }

  • // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); //...

    // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); // finds average of all //grades int findHighest (int [], int); // finds highest of all //grades int findFirstFail( int[]); int main() { int grades[100]; // the array holding 100 grades. int numberOfGrades; // the number of grades read. int pos; // index to the array. float avgOfGrades; // contains the average of the grades. int highestGrade; // contains the highest grade. int inderOfFail; //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT