Question

I need a c++ code please. 32. Program. Write a program that creates an integer constant...

I need a c++ code please.

32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94, 73, 68, 85, 48, 47, 62, 56, 75, 60, 60, 59, 77, 40, 72, 73, 95, 90, 89, 73, 93

i) void printArray(const int [] numbers). This function accepts an array as its first argument and prints each value in the array.

ii) int getSum(const int [] numbers). This function accepts an array as its argument and returns the sum of all the values in the array.

iii) double getAverage(const int [] numbers). This function accepts an array as its argument and returns the average of all the values in the array.

iv) double getMedian(const int [] numbers). This function accepts an array and returns a double which is the median (middle element) or average of the 2 middle values. (Hint: sort the array to find the median element). Also, use pointer notation * instead of array [] notation in this function.

v) int getMinValue(const int [] numbers). This function accepts an array as its argument and returns the minimum value in the array.

vi) int getMaxValue(const int [] numbers). This function accepts an array as its argument and returns the maximum value in the array.

vii) void copyArray(int [] dstnumbers, const int [] srcnumbers). This function accepts 2 arrays arguments. This function copy the value of the elements from srcnumbers array to dstnumbers array.

viii) void sortIncrease(int [] numbers). This function accepts an array as its arguments and sorts the array in increasing order.

ix) void sortDecrease(int [] numbers). This function accepts an array as it’s arguments and sorts the array in decreasing order.

x) int findNumber(const int [] numbers, int number). This function returns the index location of the number found in the array. Otherwise, it returns -1.

xi) void printNumbersLessThan(const int [] numbers, int number). This function prints the numbers of in the array that are less than the number passed in to the function.

xii) void printNumberBiggerThan(const int [] numbers, int number). This function prints the numbers in the array that is greater than the number pass in to the function. Demonstrate each of the functions in this program. Please write the prototypes of the function above main and the definitions below your main program. Note if you don’t submit your source file and/or your program does not compile, you will not receive any credit for this question.

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

CODE:

#include<iostream>
using namespace std;
const int SIZE=30;
void printArray(const int numbers[]);
int getSum(const int numbers[]);                         //prototypes of functions
double getAverage(const int numbers[]);
double getMedian(int numbers[]);
int getMinValue(const int numbers[]);
int getMaxValue(const int numbers[]);
void copyArray(int dstnumbers[], const int srcnumbers[]);
void sortIncrease(int numbers[]);
void sortDecrease(int numbers[]);
int findNumber(const int numbers[], int number);
void printNumbersLessThan(const int numbers[], int number);
void printNumberBiggerThan(const int numbers[], int number);
int main()
{
   int number;
   int numbers[SIZE]={68,100,43,58,76,72,46,55,92,94,73,68,85,48,47,62,56,75,60,60,59,77,40,72,73,95,90,89,73,93};
   printArray(numbers);
   cout<<endl;
   cout<<"sum of the Array is:"<<getSum(numbers)<<endl;
   cout<<"Average of the Array is:"<<getAverage(numbers)<<endl;
   double m=getMedian(numbers);
   cout<<"median value is:"<<m<<endl;
   cout<<"Minimum value is:"<<getMinValue(numbers)<<endl;
   cout<<"Maximum value is:"<<getMaxValue(numbers)<<endl;
   cout<<"After sorting array in ascending order:- "<<endl;
   sortIncrease(numbers);
   cout<<endl;
   cout<<"After sorting array in decending order:- "<<endl;
   sortDecrease(numbers);
   cout<<endl;
   cout<<"Enter the number to find in array:";
   cin>>number;
   int l=findNumber(numbers,number);
   cout<<"The index of "<<number<<" is: "<<l<<endl;
   cout<<"Enter the number to print elements less than entered number:";
   cin>>number;
   printNumbersLessThan(numbers,number);
   cout<<endl;
   cout<<"Enter the number to print elements greater than entered number:";
   cin>>number;
   printNumberBiggerThan(numbers,number);
   cout<<endl;
   int dstnumbers[SIZE];
   cout<<"Copying numbers array to dstnumbers array: "<<endl;
   copyArray(dstnumbers,numbers);
  
}
void printArray(const int numbers[])           //definitions of functions below main function
{
   for(int i=0;i<SIZE;i++)
   {
       cout<<numbers[i]<<" ";      //printing elements
   }
}
int getSum(const int numbers[])
{
   int sum=0;
   for(int i=0;i<SIZE;i++)
   {
       sum=sum+numbers[i];    //calculating sum of the elements
   }
   return sum;  
}
double getAverage(const int numbers[])
{
   double sum=0,avg=0;
   for(int i=0;i<SIZE;i++)
   {
       sum=sum+numbers[i];
   }
   avg=sum/SIZE;    //calculating average of elements
   return avg;  
}
double getMedian(int numbers[])
{
   int *p=numbers;
   int temp=0,i=0,j=0;
   for(i=0;i<SIZE;i++)
   {
       for(j=0;j<SIZE-1;j++)
       {
           if(*(p+j)>*(p+(j+1)))
           {
               temp=*(p+j);
               *(p+j)=*(p+(j+1));
               *(p+(j+1))=temp;
           }
       }
   }
   int mid=SIZE/2;
   double median=(*(p+(mid-1))+*(p+(mid)))/2.0;
   return median;
}
int getMinValue(const int numbers[])
{
   int min=numbers[0];
   for(int i=0;i<SIZE;i++)
   {
       if(numbers[i]<=min)
       {
           min=numbers[i];   //logic for finding minimum value
       }
   }
   return min;
}
int getMaxValue(const int numbers[])
{
   int max=numbers[0];
   for(int i=0;i<SIZE;i++)
   {
       if(numbers[i]>=max)
       {
           max=numbers[i]; //logic for finding maximum value
       }
   }
   return max;
}
void copyArray(int dstnumbers[], const int srcnumbers[])
{
   for(int i=0;i<SIZE;i++)
   {
       dstnumbers[i]=srcnumbers[i]; //copying srcnumber array to dstnumbers array
   }
   for(int i=0;i<SIZE;i++)
   {
       cout<<dstnumbers[i]<<" ";
   }
}
void sortIncrease(int numbers[])
{
   int temp=0,i=0,j=0;
   for(i=0;i<SIZE;i++)    //logic for sorting ascending order
   {
       for(j=0;j<SIZE-1;j++)
       {
           if(numbers[j]>numbers[j+1])
           {
               temp=numbers[j];
               numbers[j]=numbers[j+1];
               numbers[j+1]=temp;
           }
       }
   }
   for(int i=0;i<SIZE;i++)
   {
       cout<<numbers[i]<<" ";
   }  
}
void sortDecrease(int numbers[])
{
   int temp=0,i=0,j=0;
   for(i=0;i<SIZE;i++)   //logic for sorting decending order
   {
       for(j=0;j<SIZE-1;j++)
       {
           if(numbers[j]<numbers[j+1])
           {
               temp=numbers[j];
               numbers[j]=numbers[j+1];
               numbers[j+1]=temp;
           }
       }
   }
   for(int i=0;i<SIZE;i++)
   {
       cout<<numbers[i]<<" ";
   }  
}
int findNumber(const int numbers[], int number)
{
   int flag=0,loc=0;
   for(int i=0;i<SIZE;i++)
   {
       if(number==numbers[i])     //logic for finding index of the number
       {
           flag=1;
           return i;
       }
   }
   if(flag==0)
   {
       return -1;
   }
}
void printNumbersLessThan(const int numbers[], int number)
{
   for(int i=0;i<SIZE;i++)
   {
       if(numbers[i]<number)
       {
           cout<<numbers[i]<<" ";
       }
   }
}
void printNumberBiggerThan(const int numbers[], int number)
{
   for(int i=0;i<SIZE;i++)
   {
       if(numbers[i]>number)
       {
           cout<<numbers[i]<<" ";
       }
   }
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
I need a c++ code please. 32. Program. Write a program that creates an integer constant...
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
  • Write a program that performs the following operations on a one dimensional array with 50 unsigned...

    Write a program that performs the following operations on a one dimensional array with 50 unsigned integers. The main program will initialize the array, print the array values to the screen and then call a function that will determine and print the maximum and minimum values. •Declare the array and any other variables. •Use a loop to initialize the array with 50 random integer values between 0 and 99 using the rand() function. (number = rand() % 100;) •Using cout...

  • I need help with my homework please. I should write this program in C++ language and...

    I need help with my homework please. I should write this program in C++ language and use Inventory.h file (header file), Inventory.cpp file (implementation file), and main.cpp file (main program). This is the program: Demonstrate the class in a driver program. Input validation, don’t accept negative values for item number, quantity, or cost. 6. Inventory Class Design an Inventory class that can hold information and calculate data for items ma retail store's inventory. The class should have the following private...

  • ***Please complete the code in C*** Write a program testArray.c to initialize an array by getting...

    ***Please complete the code in C*** Write a program testArray.c to initialize an array by getting user's input. Then it prints out the minimum, maximum and average of this array. The framework of testArrav.c has been given like below Sample output: Enter 6 numbers: 11 12 4 90 1-1 Min:-1 Max:90 Average:19.50 #include<stdio.h> // Write the declaration of function processArray int main) I int arrI6]i int min-0,max-0 double avg=0; * Write the statements to get user's input and initialize the...

  • 1. The following program calls the function countLarger that accepts three arguments: an integer array, an...

    1. The following program calls the function countLarger that accepts three arguments: an integer array, an integer size that indicates how many elements are in the array, and an integer n. The function countLarger should return the number of integers in the array that are greater than the value of the argument n. Update the program to include the definition of the function countLarger. #include <iostream> using namespace std; int countLarger(int[], int, int); // Function prototype int main() const int...

  • I need help as quick as possible, thanks beforehand. Please provide the test output The Lo...

    I need help as quick as possible, thanks beforehand. Please provide the test output The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. 35 The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 - 9 exactly The sum of each row, each column and each diagonal all add up to the same number. This is shown below: 15 4 92 15 - 81 + 15 15 15...

  • in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...

    in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...

  • (C++) Write a function that accepts an int array and the array’s size as arguments. The function...

    (C++)Write a function that accepts an int array and the array’s size as arguments.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 pointer to the new array.Demonstrate the function by using it in a main program that reads an integer N (that is not more...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • P1) Write a complete C program that prints out the word YES, if its string command...

    P1) Write a complete C program that prints out the word YES, if its string command line argument contains the sequence the somewhere in it. It prints out the word NO otherwise. Both the word the and partial sequences like in the words theatre or brother qualify. Note: You can use string functions or not but if you do the only ones allowed are strcpy and strlen. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P2) What is the output of the following program (one answer per...

  • Language C Code Write a program that takes two integer arrays (A and B) and sums...

    Language C Code Write a program that takes two integer arrays (A and B) and sums them together (element wise). A third array to accept the result should be passed in as the output argument. Assume the arrays are all the same size. The argument N is the size of the arrays. Your code should provide a function with the following signature: void array Addition (int A[], int B[], int N, int output[]) { } Your code must also provide...

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