Question

2. Write a C++ program, that generates a set of random integers and places them in an array. The number of values generated aThe functions that you define should have the same prototypes and work as described above. Note that the find” functions do

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

#include<bits/stdc++.h>
using namespace std;

void print(int arr[],int total)
{
    for(int i=0;i<total;i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}
void populate(int arr[],int total,int low ,int high)
{
    int value;
    for(int i=0;i<total;i++)
   {
       cin>>value;
       if(value<low||value>high)
       {
           cout<<"Enter value in given range"<<endl;
           i=i-1;

       }
       else
       {
           arr[i]=value;
       }
   }

}

void find_greater(int arr[],int result [],int val,int total,int &result_total)
{
    int k=0;
    for(int i=0;i<total;i++)
    {
        if(arr[i]>val)
        {
            result[result_total++]=arr[i];
        }
    }

}

void find_less(int arr[],int result [],int val,int total,int &result_total)
{
    int k=0;
    for(int i=0;i<total;i++)
    {
        if(arr[i]<val)
        {
            result[result_total++]=arr[i];
        }
    }

}

void find_odd(int arr[],int result [],int total,int &result_total)
{
    int k=0;
    for(int i=0;i<total;i++)
    {
        if(arr[i]%2!=0)
        {
            result[result_total++]=arr[i];
        }
    }

}

void find_even(int arr[],int result [],int total,int &result_total)
{
    int k=0;
    for(int i=0;i<total;i++)
    {
        if(arr[i]%2==0)
        {
            result[result_total++]=arr[i];
        }
    }

}

int main()
{
   int total,low,high;
   cout<<"Enter the number of element ";
   cin>>total;
   cout<<"Enter the lowest possible value ";
   cin>>low;
   cout<<"Enter the highest possible value ";
   cin>>high;

   int arr[40],result[40];
   int result_total=0;
   populate(arr,total,low,high);

char input;
int val;
while(1)
{
      cout<<"Enter one o a (all),g (greater), l (less), e (even), o (odd), q(quit) ";
      cin>>input;
      switch(input)
    {
        case 'a':
            print(arr,total);
            break;

        case 'g':
            cout<<"Enter the value ";
            cin>>val;
            find_greater(arr,result,val,total,result_total=0);
            print(result,result_total);
            break;

        case 'l':
            cout<<"Enter the value ";
            cin>>val;
            find_less(arr,result,val,total,result_total=0);
            print(result,result_total);

            break;

        case 'e':
            find_even(arr,result,total,result_total=0);
            print(result,result_total);

            break;

        case 'o':
            find_odd(arr,result,total,result_total=0);
            print(result,result_total);

            break;

        case 'q':
            return 0;
            break;

        default:
            cout<<"Must enter one of a,g,l,e,o or q"<<endl;
    }

}

}


output-

C:\Users\hp\Documents\check.exe Enter the number of element 3 Enter the lowest possible value 5 Enter the highest possible va

Add a comment
Know the answer?
Add Answer to:
2. Write a C++ program, that generates a set of random integers and places them in...
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 C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • Write a C program to do the following 1) request user to enter 10 integer into...

    Write a C program to do the following 1) request user to enter 10 integer into an array 2) sort the array in ascending order 3) display the sorted array 4) count the number of odd and even number in the array and print out the result 5) add the value of the odd number and even number and calculate the average of the odd and even number. display the result 6) write function addNumber() to add all the number...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • Write array methods that carry out the following tasks for an array of integers by completing...

    Write array methods that carry out the following tasks for an array of integers by completing the ArrayMethods class below. For each method, provide a test program. public class ArrayMethods { private int[] values; public ArrayMethods(int[] initialValues) { values = initialValues; } public void swapFirstAndLast() { . . . } public void shiftRight() { . . . } .. . } a. Swap the first and last elements in the array. b. Shift all elements to the right by one...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration...

    JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...

  • Write a program that finds (ANSWER IN C LANGUAGE!): 1. The sum of all elements at...

    Write a program that finds (ANSWER IN C LANGUAGE!): 1. The sum of all elements at even subscripts 2. The sum of all elements at odd subscripts 3. The sum of all elements You are allowed to perform this functionality within main. Main program: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #include <stdio.h> int main() { /* Declare array variables */ const int NUM_VALS = 4; int userValues[NUM_VALS]; int i; // counter int even = 0; int odd = 0; int sum = 0; /* Initialize...

  • Write a Program in C language for: 1. Create a C program to read 7 integers...

    Write a Program in C language for: 1. Create a C program to read 7 integers and store them in an array. Next, the program is to check if the array is symmetric, that is if the first element is equal to the last one, the value of the second one is equal to the value of the last but one, and so on. Since the middle element is not compared with another element, this would not affect the symmetry...

  • LAB: How many negative numbers Write a program that generates a list of integers, and outputs...

    LAB: How many negative numbers Write a program that generates a list of integers, and outputs those integers, and then prints the number of negative elements. Create a method (static void fillArray(int [] array)) that uses Random to generate 50 values (Random.nextInt()), put each value in the array (make sure to pass the array in to this method as an argument). Write another method (static int printAndCountNeg(int [] array)) that prints each element’s value and count the number of negative...

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