Home > database >  functions giving me garbage
functions giving me garbage

Time:11-21

I’ve created an array to get the sum, biggest number, and smallest number from all the numbers in an array. When I use a cout statement to print out the numbers I’m given garbage. Can anyone help me understand what went wrong and what to do

I previously asked a question on the same code asking about why my array wouldn’t print but I’ve fixed that problem so now the arrays are printing but after adding more functions to analyze the array and get info from it the outputs from the functions are garbage. Here’s what I have:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;

const int Ro = 4; 
const int Co = 5;



void fillArray(int myArray[Ro][Co]);
void printArray(int myArray[Ro][Co]);
void doubleArray(int myArray[Ro][Co]);
int max(int myArray[Ro][Co]);
int min(int myArray[Ro][Co]);
int sum(int myArray[Ro][Co]);



int main() {
 srand(time(NULL));
  int myArray[Ro][Co];
  int sum, max, min;
  
  
cout << "\n\n\n";
  fillArray(myArray);
  printArray(myArray);
  
  cout << "\n";
  cout << "The largest number is : " << max;
  cout << "\n";
  cout << "The smallest number is : " << min;
  cout << "\n";
  cout << "The sum is : " << sum;
  
  
  cout << "\n\n";
  doubleArray(myArray);
  printArray(myArray);
  return 0;
}


void fillArray(int myArray[Ro][Co]){
  
for (int r = 0; r <= Ro; r  )
{
  for(int c = 0; c <= Co; c  ){

  myArray[r][c] = (rand()0);
    }
  }
}
void printArray(int myArray[Ro][Co]){
  for(int r = 0; r < Ro; r  ){
    for(int c = 0; c < Co; c  ){
      cout << myArray[r][c] << " "; 
    }
    cout << "\n";
  }
}
void doubleArray(int myArray[Ro][Co]){
  for(int r = 0; r < Ro; r  ){
    for(int c = 0; c < Co; c  ){
      myArray[r][c] = myArray[r][c]*2;
    }
  }
}
int max(int myArray[Ro][Co])
{
  int max = myArray[0][0];
  for(int r = 0; r < Ro; r  )
    {
    for(int c = 0; c < Co; c  )
      {
        if(myArray[r][c] > max){
          max = myArray[r][c];
        }
      }
   }
  return max;
}
int min(int myArray[Ro][Co])
{
 int min = myArray[0][0];
  for(int r = 0; r < Ro; r  )
    {
    for(int c = 0; c < Co; c  )
      {
        if(myArray[r][c] < min){
          min = myArray[r][c];
        }
      }
   } 
  return min;
}
int sum(int myArray[Ro][Co])
{
  int sum = 0;
  for(int r = 0; r < Ro; r  )
    {
    for(int c = 0; c < Co; c  )
      {
        sum  = myArray[r][c];
        }
      }
  return sum;
}

CodePudding user response:

as i pointed out in a comment , you run off the end of every array all the time

for (int r = 0; r <= Ro; r  )
{
  for(int c = 0; c <= Co; c  ){

  myArray[r][c] = (rand()0);
    }
  }
}

should be

for (int r = 0; r < Ro; r  )
{
  for(int c = 0; c < Co; c  ){

  myArray[r][c] = (rand()0);
    }
  }
}

CodePudding user response:

You are actually using uninitialized variables rather than calling the functions of the same name. You have both functions and variables named max, min and sum. The variables have garbage values because they weren't initialized.

If you intend to call the function rather than use the variable, then you need to place parentheses after the function name with the needed arguments inside parentheses, like this:

  max = max(myArray);
  min = min(myArray);
  sum = sum(myArray);
  cout << "\n";
  cout << "The largest number is : " << max;
  cout << "\n";
  cout << "The smallest number is : " << min;
  cout << "\n";
  cout << "The sum is : " << sum;
  • Related