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

Please help getting error message on putty read below //I am running this on vis

ID: 3748796 • Letter: P

Question

 Please help getting error message on putty read below  //I am running this on visual studio and its running perfectly fine. //WHen i try to run it on putty, im getting an error message for  //the max and min //saying : INT_MAX and INT_MIN were not declared in this scope //Please help !        #include <iostream>     #include <fstream> // To use ifstream          #define ARRAY_SIZE 10     using namespace std;     void readData(int numbers[]) {         ifstream inputFile;        // Input file stream object         // Open the file.         inputFile.open("arrays.txt");         int index = 0;         // Read the numbers from the file into the array.         while (inputFile >> numbers[index] && index < 10) {                 index++;         }              // Close the file.         inputFile.close();     }          void printArray(int numbers[]) {         for (int i = 0; i < ARRAY_SIZE; i++) {                 cout << numbers[i] << " ";         }         cout << endl;          }          void reverseArray(int numbers[]) {         for (int i = ARRAY_SIZE - 1; i >= 0; i--) {                 cout << numbers[i] << " ";         }         cout << endl;     }          void minMax(int numbers[]) {         int minNumber = INT_MAX;         int maxNumber = INT_MIN;         //return the minimum and the maximum         for (int i = 0; i < ARRAY_SIZE; i++) {                 if (numbers[i] > maxNumber) {                         maxNumber = numbers[i];                 }                      if (numbers[i] < minNumber) {                         minNumber = numbers[i];                 }         }              cout << "Maximum number = " << maxNumber << endl;         cout << "Minimum number = " << minNumber << endl;     }               int main() {              int numbers[ARRAY_SIZE];   // Array number with 10 elements         readData(numbers);                   // Display the numbers read:         cout << "The numbers in the array are the following: " << endl;         printArray(numbers);              //display the reverse order         cout << "The array in reverse order looks like the following: " << endl;         reverseArray(numbers);              // Display the numbers read:         cout << "The numbers in the array are the following: " << endl;         printArray(numbers);              minMax(numbers);         getchar();              return 0;          }

Explanation / Answer

Solution 1)

The INT_MIN and INT_MAX both are macros defined in #include <limits.h> in C & C++. The values of both these macros are compiler dependent because sizeof(int)is implementation defined in both C & C++.  INT_MIN & INT_MAX macro defines minimum & maximum value for an int.

so include the library file

#include <iostream>

#include <climits>

#include <fstream>

Then it should work

Solution 2)

include the library

#include <iostream>

#incldue <bits/stdc++.h>

#include <fstream>

My Running code :-

#include <iostream>
#include <fstream> // To use ifstream
#include <climits>
  

#define ARRAY_SIZE 10
using namespace std;
void readData(int numbers[]) {
ifstream inputFile; // Input file stream object
// Open the file.
inputFile.open("arrays.txt");
int index = 0;
// Read the numbers from the file into the array.
while (inputFile >> numbers[index] && index < 10) {
index++;
}
  
// Close the file.
inputFile.close();
}
  
void printArray(int numbers[]) {
for (int i = 0; i < ARRAY_SIZE; i++) {
cout << numbers[i] << " ";
}
cout << endl;
  
}
  
void reverseArray(int numbers[]) {
for (int i = ARRAY_SIZE - 1; i >= 0; i--) {
cout << numbers[i] << " ";
}
cout << endl;
}
  
void minMax(int numbers[]) {
int minNumber = INT_MAX;
int maxNumber = INT_MIN;
//return the minimum and the maximum
for (int i = 0; i < ARRAY_SIZE; i++) {
if (numbers[i] > maxNumber) {
maxNumber = numbers[i];
}
  
if (numbers[i] < minNumber) {
minNumber = numbers[i];
}
}
  
cout << "Maximum number = " << maxNumber << endl;
cout << "Minimum number = " << minNumber << endl;
}
  
  
int main() {
  
int numbers[ARRAY_SIZE]; // Array number with 10 elements
readData(numbers);
  
  
// Display the numbers read:
cout << "The numbers in the array are the following: " << endl;
printArray(numbers);
  
//display the reverse order
cout << "The array in reverse order looks like the following: " << endl;
reverseArray(numbers);
  
// Display the numbers read:
cout << "The numbers in the array are the following: " << endl;
printArray(numbers);
  
minMax(numbers);
getchar();
  
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