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
  • Create a program that creates an array of 500 random numbers between -10 and 10. Write...

    Create a program that creates an array of 500 random numbers between -10 and 10. Write several functions: Function 1 - prints the array with a certain number of elements per line. Prototype: void printArray(int array[], int numElements, int numPerLine, ostream &os) Function 2 prints only the array elements that are evenly divisible by an input number. Prototype: void printDivBy(int array[], int numElements, int numPerLine, int divBy, ostream &os) Function 3 takes the input array and returns the maximum, the...

  • 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...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print 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...

  • Please help with this coding 1.You need to dynamically allocate memory to read into a number...

    Please help with this coding 1.You need to dynamically allocate memory to read into a number of elements. At first, you need to determine how many numbers (here, ‘n’) to be read. Next, you need to dynamically allocate memory for ‘n’ integers. As you see,‘x’ is an integer pointer which needs to dynamically allocate memory to read ‘n’integers into it fromthekeyboard. 2 Read nintegers into the allocated block using scanf. 3. Complete the printArrayfunction to display ‘n’ integers. void printArray(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...

  • Write a program in C that creates an array of 100 random numbers from 0-99. The...

    Write a program in C that creates an array of 100 random numbers from 0-99. The program sums the random numbers and prints the sum.  It then writes the numbers to a new file using open, close and write. Then looks in the current directory for files that match the pattern “numbers.XXXX”. For each file, open the file and read the file. You can assume that the file will contain 100 integers. Sum the integers. Print the filename and the sum...

  • 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...

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