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

Write a program that will read in a list of 25 numbers. Sort the list of numbers

ID: 3632652 • Letter: W

Question

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. The numbers are coming from a data file.

I have program printing out the numbers that are found. How do I get the program to print out the numbers that are not found in a format similar to that for those that are found?

#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
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
cout <<" ";
cout <<"The numbers entered in ascending order are"<<endl;
for(i = 0; i < arraySize; ++i){
cout <<arr[i]<<" ";
}//for i
cout <<" ";

int item;

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<<" FOUND"<<endl;
break;
}//if the midpoint is the number sought
}//Finding the middle
}// for input while




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