Question

In: Computer Science

C Language - Programming Write a function that takes an array of ints, and the size...

C Language - Programming

  1. Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92}
  2. Write a function that takes one double parameter, and returns a char. The parameter represents a grade, and the char represents the corresponding letter grade. If you pass in 90, the char returned will be ‘A’. If you pass in 58.67, the char returned will be an ‘F’ etc. Use the grading scheme on the syllabus for this course to decide what letter to return.
  3. Write a function that takes 3 int arguments and returns the largest of the 3.

Can you please post the output here as well Thank you

C Language

Solutions

Expert Solution

A)

Source code:

#include <stdio.h>
double average() /*here we create function average for calculate average of array*/
{
int n,i,total=0;
double avg;
printf("Enter the size of array\n");
scanf("%d",&n); /*here we take size of array from user*/
int a[n];
printf("Enter the element of array\n");
for(i=0;i<n;i++)
{   
scanf("%d",&a[i]); /*here we take value of array from user*/
total=total+a[i]; /*here we do sum of element in array*/
}
avg=total/n; /*here we calculate average*/
return avg; /*here we return avg*/
}
int main()
{
double value;
value=average(); /*here we call average function*/
printf("average of entered element in array is %lf",value);
return 0;
}

output:

B)

Source code:

#include <stdio.h>
char grade(double c) /*create function for decide grade*/
{ double a;
a=c;
char ch;
if(a>=90) /*check if a >90 than return A*/
{
ch='A';
return ch;
}
if(a<90 && a>80) /*check if a <=90 and a>80 than return B*/
{
ch='B';
return ch;
}
if(a<=80 && a>70) /*check if a <=80 and a>70 than return C*/
{
ch='C';
return ch;
}
if(a<=70 && a>65) /*check if a <=70 and a>65 than return D*/
{
ch='D';
return ch;
}
if(a<=65 && a>58.67) /*check if a <=65 and a>58.67 than return E*/
{
ch='E';
return ch;
}
if(a<=58.67) /*check if a <=58.67 than return F*/
{
ch='F';
return ch;
}

}
int main()
{ char grade(double b1);
double s;
char g;
printf("Enter a Score :");
scanf("%lf",&s); /*take input from user for getting score */
g=grade(s); /*call function and store value in g*/
printf("Your grade is : %c ",g); /*print value of g*/
return 0;
}

output:

c)

source code:

#include <stdio.h>
double largest(int a,int b,int c) /*here we make a function for find the largest*/
{
if( a>=b && a>=c ) /*check a is >b & c than return a */
return a;
if( b>=a && b>=c ) /*check b is >a & c than return b */
return b;
if( c>=a && c>=b ) /*check c is >a & b than return c */
return c;
}
int main()
{
int large_V,x,y,z;
printf("Enter 3 values:\n");
scanf("%d",&x); /*take value from user*/
scanf("%d",&y); /*take value from user*/
scanf("%d",&z); /*take value from user*/
large_V=largest(x,y,z); /*here we call function*/
printf("Largest number is %d",large_V);
return 0;
}

output:


Related Solutions

In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another C++ function,lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Also, write a program to test your function. You must write our commands in the middle: Write a C++ Program: #include <iostream> using namespace std; const int ARRAY_SIZE = 15; void printArray(const int x[], int sizeX); int smallestIndex(const int x[], int sizeX); int main() {      int list[ARRAY_SIZE] = {56,...
Programming in C language (not C++) Write a function definition called PhoneType that takes one character...
Programming in C language (not C++) Write a function definition called PhoneType that takes one character argument/ parameter called "phone" and returns a double. When the variable argument phone contains the caracter a or A, print the word Apple and return 1099.99. When phone contains the caracter s or S print the word Samsung and return 999.99. When phone contains anything else, return 0.0.
Write a C++ function which takes an int array and its size as parameters. It returns...
Write a C++ function which takes an int array and its size as parameters. It returns an int indicating how many multiples of 3 are contained in the array. For example, if the array contains {2, 6, 8} your function should return 1. If the array contains {3, 10, 5, 6} your function should return 2. Here is the function prototype: // int array[]: array to search // size: number of elements in the array int countMult(const int array[], int...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that will use a while loop. The function will prompt the user to enter integers ine by one, until the user enters a negative value to stop. The function will display any integer that is less than 25. Declare and initialize any variables needed. The function takes no arguments and has a void return type.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT