Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My homework reads as follows: Write a program that will read in a list of 25 num

ID: 3632387 • Letter: M

Question

My homework reads as follows:

Write a program that will read in a list of 25 numbers.
Sort the list of numbers. On the next line, will be a list of numbers to search the list for.

My program as written does what is required but I would like it to also print out the phrase "The numbers found are... " before the list of numbers.

#include <iostream>
using namespace std;

int main ()
{
int i, hold;
const int arraySize = 25;
int arr[arraySize];

for(i = 0; i < arraySize; i++){
cin >>arr[i];
}//fill the array

cout <<"The numbers in the array are"<<endl;
for(i = 0; i < arraySize; i++){
cout <<arr[i]<<" ";
}//print the array original order
cout <<" ";

bool changesMade = true;
for(int pass = 0; pass < arraySize -1; ++pass){
changesMade = false;
for(int j = 0; j < arraySize -1; ++j){
if(arr[j] > arr[j + 1]){
hold = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = hold;
changesMade = true;
}// for if
}//for j
}// for pass to sort the array
cout <<" ";
cout <<"The numbers entered in ascending order are"<<endl;
for(i = 0; i < arraySize; ++i){
cout <<arr[i]<<" ";
}//for i to print the sorted array
cout <<" ";
cout <<"The numbers we are looking for are"<<endl;

int item;
int itemNumber = 0;
int numSought[itemNumber];

while (cin>>item){
int mid, low = 0, high = arraySize -1;

while (low <= high) {
mid = ((high + low)/2);
if (arr[mid] < item) {
low = mid +1;
}// if the mid point is less than the number sought
if (arr[mid] > item) {
high = mid -1;
}//if the midpoint is greater than the number sought
if (arr[mid] == item) {
cout <<item<<" ";
numSought[itemNumber] = arr[mid];
++itemNumber;
break;
}//if the midpoint is the number sought
}//Finding the middle
}// for input while

cout <<"The numbers found are ";
for(int i = 0; i < itemNumber; i++) {
cout <<numSought[i]<<endl;
}//for numbers found loop



system ("PAUSE");
return 0;
}

Explanation / Answer

Answer:

The following is an algorithm for this program using a flow chart. We can use a modulus operator to solve this problem. There will be no remainder for even number when we modulus the number by 2.

#include <stdio.h>

int main()

{

      int num = 0, remainder = 0;

   

      // while -1 not entered...

      while(num != -1)

      {

            // prompt user for input

            printf("Enter an integer (-1 to stop): ");

            // read and store input, then modulus by 2

            scanf_s("%d", &num, sizeof(int));

            // ready to stop if -1 else...

            if(num != -1)

            {

                  remainder = num % 2;

                  // test for even/odd. If the modulus yields 0, it is even

                  if(remainder == 0)

                        printf("%d is an even number. ", num);

                  else

                        printf("%d is an odd number. ", num);

            }

      }

      // -1 was entered

      printf("%d is an odd number. ", num);

      printf("You ask to stop! Thank you. ");

      return 0;

}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote