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

In C++ Make an array with 5 elements. Read the integers from the user. If the us

ID: 3715299 • Letter: I

Question

In C++
Make an array with 5 elements. Read the integers from the user. If the user enters a negative number or a letter, then throw an exception, print an error message, and exit the program. Next, use 3 pointers. One to go through each element of the array. One to point to the smallest element and one to point to the largest element. Design Notes: -declare array of 5 integers -declare 3 integer pointers – arrPtr, smallest, largest - use a loop to get 5 integers from user - if user enters negative number or letters, then throw an exception - for the catch part, simply print a message and exit main - start arrPtr at the first element of the integer array - set smallest and largest as the first element of the array - loop through elements - print the value and address of the array element - if value at arrPtr is smaller than smallest - save arrPtr to smallest - if value at arrPtr is larger than largest - save arrPtr to largest - move arrPtr to next element in the array - print the value of the smallest element - print the value of the largest element Sample Run #1: (the highlighted text is what the user types) Note that your addresses may be different, but should be 4 bytes apart from each other Number? 2 Number? 3 Number? 4 Number? 1 Number? 5 At 002CFE2C is 2 At 002CFE30 is 3 At 002CFE34 is 4 At 002CFE38 is 1 At 002CFE3C is 5 smallest is 1 at 002CFE38 largest is 5 at 002CFE3C Sample Run #2: (the highlighted text is what the user types) Number? 2 Number? x Error, you must enter an integer Sample Run #1: (the highlighted text is what the user types Number? 2 Number? 3 Number? -1 Error, the number must be positive In C++
Make an array with 5 elements. Read the integers from the user. If the user enters a negative number or a letter, then throw an exception, print an error message, and exit the program. Next, use 3 pointers. One to go through each element of the array. One to point to the smallest element and one to point to the largest element. Design Notes: -declare array of 5 integers -declare 3 integer pointers – arrPtr, smallest, largest - use a loop to get 5 integers from user - if user enters negative number or letters, then throw an exception - for the catch part, simply print a message and exit main - start arrPtr at the first element of the integer array - set smallest and largest as the first element of the array - loop through elements - print the value and address of the array element - if value at arrPtr is smaller than smallest - save arrPtr to smallest - if value at arrPtr is larger than largest - save arrPtr to largest - move arrPtr to next element in the array - print the value of the smallest element - print the value of the largest element Sample Run #1: (the highlighted text is what the user types) Note that your addresses may be different, but should be 4 bytes apart from each other Number? 2 Number? 3 Number? 4 Number? 1 Number? 5 At 002CFE2C is 2 At 002CFE30 is 3 At 002CFE34 is 4 At 002CFE38 is 1 At 002CFE3C is 5 smallest is 1 at 002CFE38 largest is 5 at 002CFE3C Sample Run #2: (the highlighted text is what the user types) Number? 2 Number? x Error, you must enter an integer Sample Run #1: (the highlighted text is what the user types Number? 2 Number? 3 Number? -1 Error, the number must be positive In C++
Make an array with 5 elements. Read the integers from the user. If the user enters a negative number or a letter, then throw an exception, print an error message, and exit the program. Next, use 3 pointers. One to go through each element of the array. One to point to the smallest element and one to point to the largest element. Design Notes: -declare array of 5 integers -declare 3 integer pointers – arrPtr, smallest, largest - use a loop to get 5 integers from user - if user enters negative number or letters, then throw an exception - for the catch part, simply print a message and exit main - start arrPtr at the first element of the integer array - set smallest and largest as the first element of the array - loop through elements - print the value and address of the array element - if value at arrPtr is smaller than smallest - save arrPtr to smallest - if value at arrPtr is larger than largest - save arrPtr to largest - move arrPtr to next element in the array - print the value of the smallest element - print the value of the largest element Sample Run #1: (the highlighted text is what the user types) Note that your addresses may be different, but should be 4 bytes apart from each other Number? 2 Number? 3 Number? 4 Number? 1 Number? 5 At 002CFE2C is 2 At 002CFE30 is 3 At 002CFE34 is 4 At 002CFE38 is 1 At 002CFE3C is 5 smallest is 1 at 002CFE38 largest is 5 at 002CFE3C Sample Run #2: (the highlighted text is what the user types) Number? 2 Number? x Error, you must enter an integer Sample Run #1: (the highlighted text is what the user types Number? 2 Number? 3 Number? -1 Error, the number must be positive

Explanation / Answer

#include <iostream>
#include <stdlib.h>
using namespace std;
const int MAX = 5;

int main ()
{
   int var[MAX];
   int *ptr[MAX], *smallest, *largest;

   for (int i = 0; i < MAX; i++)
   {
       cout<<"Number?";
       cin>>var[i];

       if(!cin)
       {
           cout<<"Error, you must enter an integer"<<endl;
           exit(0);
       }
       if(var[i] < 0)
       {
           cout<<"Error, the number must be positive"<<endl;
           exit(0);
       }
      
      
   }

   smallest = &var[0];
   largest = &var[0];

   for (int i = 0; i < MAX; i++)
   {
      ptr[i] = &var[i]; // assign the address of integer.
      if(var[i] < *smallest)
      {
       smallest = &var[i];
      }
      if(*largest < var[i])
      {
       largest = &var[i];
      }
   }

   for (int i = 0; i < MAX; i++) {
       cout << "At " <<ptr[i]<<" is "<<*ptr[i]<<endl;
   }
   cout<<"The smallest is: "<<*smallest<<" at "<<smallest<<endl;
   cout<<"The largest is: "<<*largest<<" at "<<largest<<endl;

   return 0;
}