Question

In: Computer Science

use repil.it intro to C-programin be sure Make code very basic and add good comments thank...

use repil.it

intro to C-programin be sure Make code very basic and add good comments thank you

1-Write a program that requests 5 integers from the user and stores them in an array. You
may do this with either a for loop OR by getting a string from stdin and using sscanf to store formatted input in each spot in the array.

2-Add a function to the program, called get_mean that determines the mean, which is the average of the values. The function should be passed the array and the size of the array, but use const so that it cannot be changed, only accessed. It should return a double, which is the calculated mean. The main function will print the value.

//Function prototype:
double get_mean(const int [], int);

3-Create a function called search_array that searches the array for the answer to life, the universe, and everything - the integer 42. If 42 is found, print You wrote The Answer to the Great Question!. If it is not found, print You might not know the answer to Life, the Universe, and Everything. Set a global variable called search_for to store the number to search for (42)

//Function prototype:
void search_array(const int [], int);

4-Create a function called sort_array. The array should be sorted in descending order. The function should be passed the array and the size of the array. The function does not return a value; the array is changed by the function. The main function will print the sorted array (you may create a print array function if desired).

//Function prototype:
void sort_array(int [], int);

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

#include<stdio.h>

//global variable to store the number to search for (42)
int search_for = 42;

//method to calculate and return the mean of values
double get_mean(const int array[], int size){
        double sum=0;
        //finding sum of integers on the array
        for(int i=0;i<size;i++){
                sum+=array[i];
        }
        //finding mean
        double mean=sum/size;
        return mean;
}

//method to search the array for global variable value search_for
void search_array(const int array[], int size){
        for(int i=0;i<size;i++){
                if(array[i]==search_for){
                        //found
                        printf("You wrote The Answer to the Great Question!\n");
                        return;
                }
        }
        //not found
        printf("You might not know the answer to Life, the Universe, and Everything\n");

}

//method to print contents of an array
void print_array(const int array[], int size){
        for(int i=0;i<size;i++){
                printf("%d ",array[i]);
        }
        printf("\n");
}

//method to sort the array in descending order
void sort_array(int array[], int size){
        //using bubble sort algorithm
        for(int i=0;i<size;i++){
                for(int j=0;j<size-i-1;j++){
                        if(array[j]<array[j+1]){
                                //swapping elements at j and j+1
                                int temp=array[j];
                                array[j]=array[j+1];
                                array[j+1]=temp;
                        }
                }
        }

}

int main(){
        const int size=5;
        //creating an array of size 5
        int array[size];
        
        //asking and reading values into the array
        printf("Enter %d integers: ",size);
        for(int i=0;i<size;i++){
                scanf("%d",&array[i]);
        }
        
        //finding, displaying mean
        double mean=get_mean(array,size);
        printf("Mean value: %.2f\n",mean);

        //searching for 42
        search_array(array,size);
        
        //sorting and displaying sorted array
        sort_array(array,size);
        printf("Array sorted in descending order: ");
        print_array(array,size);
        return 0;       
}

/*OUTPUT*/

Enter 5 integers: 23 42 88 34 99
Mean value: 57.20
You wrote The Answer to the Great Question!
Array sorted in descending order: 99 88 42 34 23

Related Solutions

Also please add comments on the code and complete in C and also please use your...
Also please add comments on the code and complete in C and also please use your last name as key. The primary objective of this project is to increase your understanding of the fundamental implementation of Vigenere Cipher based program to encrypt any given message based on the Vignere algorithm. Your last name must be used as the cipher key. You also have to skip the space between the words, while replicating the key to cover the entire message. Test...
​​​​​​​in c code Add an intro screen Ask the user for their name, what type of...
​​​​​​​in c code Add an intro screen Ask the user for their name, what type of cookie they would like to order and how many. The types are sugar, chocolate chip, and peanut butter. Assign a cost to each cookie. Show total for cookie purchase and ask if the would like to place another order. Include at least one function and one loop. Add one extra feature. For example, a sample execution of your code would be as follows: Cookie...
C++ CODE PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT 1....
C++ CODE PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT 1. You must call all of the defined functions above in your code. You may not change function names, parameters, or return types. 2. You must use a switch statement to check the user's menu option choice. 3. You may create additional functions in addition to the required functions listed above if you would like. 4. If user provides option which is not a choice...
For this week, I want to make sure you have a good, basic grounding in the...
For this week, I want to make sure you have a good, basic grounding in the principles of central tendency. Most statistics operates around two principles: central tendency and dispersion. Central tendency tells us where the "middle" of a set of data is, while "dispersion" allows us to see how spread out the data are from the middle. This week, we are covering central tendency. Your first assignment is to use the Excel Data Analysis add-in (or a calculator or...
Make sure to include comments that explain all your steps (starts with #) Make sure to...
Make sure to include comments that explain all your steps (starts with #) Make sure to include comments that explain all your steps (starts with #) Write a program that prompts the user for a string (a sentence, a word list, single words etc.), counts the number of times each word appears and outputs the total word count and unique word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt...
make multiply function with ‘add’ command, Convert to mips code Don’t use ‘mult’ Use 'add' multiple...
make multiply function with ‘add’ command, Convert to mips code Don’t use ‘mult’ Use 'add' multiple times Get input from key-board and display the result in the console window
Java Code - Please explain each step through comments. Also, make sure Main (P3_13) is separate...
Java Code - Please explain each step through comments. Also, make sure Main (P3_13) is separate from your class BetterRectangle. Lastly, post a picture of your code inside an IDE along with your output. My code is below, needs modification. E9.13The java.awt.Rectangle class of the standard Java library does not supply a method to compute the area or perimeter of a rectangle. Provide a subclass BetterRectangle ofthe Rectangle class that has getPerimeter and getArea methods. Do not add any instance...
Please explain each step very carefully and make sure your handwriting is easy to read. Thank...
Please explain each step very carefully and make sure your handwriting is easy to read. Thank you Question: Suppose p(x) is a polynomial of degree n with coefficients in R and suppose p(x) has exactly n real roots. Show that p'(x) has exactly n-1 real roots.
Please Please fulfill the requirements and error handling and make this in basic C++ construct Thank...
Please Please fulfill the requirements and error handling and make this in basic C++ construct Thank you!! Objectives: Perform C++ string object manipulation Understand how to manipulate data using arrays Handle input errors and invalid values Design and create a well-structure program using C++ basic programming constructs Description: Write a menu-driven program that provides the following options: Show All Spend Search expenses containing this string Search expenses with greater than or equal to this amount Exit It allows the user...
Code the following in C++ and make sure to include all three files required at the...
Code the following in C++ and make sure to include all three files required at the end. Hotdogs – The World’s Greatest Cost Effective Wholesome Nutritious Food Due to world food supply scarcity and the burgeoning populations of the world’s countries, your hot stand business is globalizing to satisfy the world’s desire for a cost effective wholesome nutritious food source. Market studies have shown that tens of billions of people are craving for the eponymous hotdog which will result in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT