-3

I am fairly new to stackoverflow and coding, so I do not know what to do at this point. As the tile mentions, this code just does not output anything. It only runs, but with no output. I do not know what to do at this point. I am using Visual Studio Code with g++. And yes I have checked that the input file is placed correctly. What could be the problem?

#include <iostream>
#include <fstream>      
using namespace std;
const int NR = 10;
const int NC = 5;
int getTotal(int arr[NR][NC]);
float getAverage (int arr[NR][NC]);
int rowTotal (int arr[NR][NC], int numOfRow);
int colTotal (int arr[NR][NC], int numOfCol);
int getHighestInRow (int arr[NR][NC], int numOfRow);
int getLowestInRow (int arr[NR][NC], int numOfRow);

int main()
{
ifstream file("arraysfile.txt");

 int arr[NR][NC];
 int total = 0;
 float avg = 0.0;
 int rowTotal = 0;
 int colTotal = 0;
 int highestInRow = 0;
 int lowestInRow = 0;
 int numOfRow = 0;
 int numOfCol = 0;
   for (int r = 0; r < 10; r++)
   {
     for (int c = 0; c < 5; c++)
     {
     file >> arr[10][5];
     }
   }
   total = getTotal(arr);
   avg = getAverage(arr);
   cout <<"What row would you like to process?"<<std::endl;
   cin >> numOfRow;
   cout <<"What column would you like to process?"<<std::endl;
   cin >> numOfCol;
   rowTotal = (arr, numOfRow);
   colTotal = (arr, numOfCol);
   highestInRow = getHighestInRow(arr, numOfRow);
   lowestInRow = getLowestInRow(arr, numOfRow);
   cout << "Total sum of all number is: "<<total<<std::endl;
   cout << "Average of all numbers is: "<< avg <<std::endl;
  cout << "Sum of row # "<<numOfRow<< " is: "<<rowTotal<<std::endl;
  cout << "Sum of column # "<<numOfCol<< " is: "<<colTotal<<std::endl;
  cout << "The highest number in row # "<<numOfRow<< " is: "<<highestInRow<<std::endl;
  cout << "The lowest number in row # "<<numOfRow<< " is: "<<lowestInRow<<std::endl;
 
}
int getTotal(int arr[NR][NC])
{
  int total = 0;
  for (int i = 0; i < NR; i++)
  {
    for (int j = 0; i <NC; j++)
    {
      total+=arr[i][j];
    }
  }
  return total;
}
float getAverage (int arr[NR][NC])
{
  float avg = 0.0;
  int total = 0;
  for (int i = 0; i < NR; i++)
  {
    for (int j = 0; i <NC; j++)
    {
      total+=arr[i][j];
    }
  }
  avg = (total/(NR/NC));
  return avg;
}
int rowTotal (int arr[NR][NC], int numOfRow)
{
  int rowTotal = 0;
  for (int i = 0; i < NC; i++)
  {
    rowTotal += arr[numOfRow-1][i];
  }
  return rowTotal;
}
int colTotal (int arr[NR][NC], int numOfCol)
{
  int colTotal = 0;
  for (int i = 0; i < NR; i++)
  {
    colTotal += arr[i][numOfCol-1];
  }
  return colTotal;
}
int getHighestInRow (int arr[NR][NC], int numOfRow)
{
  int highest = arr[numOfRow][0];
  for (int i = 0; i < NC; i++)
  {
    if (arr[numOfRow][i] > highest)
    {
      highest = arr[numOfRow][i];
    }
  }
  return highest;
}
int getLowestInRow (int arr[NR][NC], int numOfRow)
{
  int lowest = arr[numOfRow][0];
  for (int i = 0; i < NC; i++)
  {
    if (arr[numOfRow][i] < lowest)
    {
      lowest = arr[numOfRow][i];
    }
  }
  return lowest;
}
rioV8
  • 18,123
  • 3
  • 18
  • 35
andrej
  • 1
  • 1
  • 2
    `file >> arr[10][5];` -> `file >> arr[r][c];` this only could explain anything, because you are trying to write into an array element out of the bounds of the array. Your code has undefined behavior – 463035818_is_not_a_number Sep 18 '21 at 20:27
  • 4
    Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) – Andreas Wenzel Sep 18 '21 at 20:27
  • For starters, build with warnings enabled and treat them as errors that must be fixed. [Your code have some warnings](https://godbolt.org/z/3EEn6jca3) about things that might now work as you expect. – Some programmer dude Sep 18 '21 at 20:29
  • You’re using visual studio. Are you using a debugger? – gnasher729 Sep 18 '21 at 21:08

1 Answers1

0

If I start putting comments in, comment list would be long, so lets start what is wrong with your code:

Not checking if your file is open

ifstream file("arraysfile.txt");

if (!file())
    cerr << "error opening file"

As everybody else pointed out

 file >> arr[r][c];

I thing this is also a problem

avg = (total/(NR/NC));

it should be total / <numberofcells> or (NR*NC)

Another point is use constexpr instead of const. const is runtime constant, where as constexpr is compile time constant value.