0

I'm working on a program in C++ that is supposed to read in a file, store the content of the file into a 2D array, assign characters to each of the numbers in the array and store in a char array, and print both of these arrays. It's then supposed to go through the initial array and make sure that each number doesn't differ in value from it's neighboring numbers by more than 1, correct these errors by replacing these numbers with the value of the average of their neighbors, assign characters to this corrected array as it did before, and print both arrays.

The character assignments go as follows:

0=blank
1=.
2=,
3=_
4=!
5=+
6=*
7=#
8=$
9=&

I have the code written that opens the file and loads the array, but I have no idea where to go from there. To me the obvious, although probably not best, way to do the assignments is to go through the array with a for loop and use a series of if statements to check for the value of the number at each index and assign the appropriate symbol. I'm sure there's a better way to accomplish this.

Here is the code I have so far:

#include <iostream>
#include <fstream>
using namespace std;

int main() {
ifstream prog;
prog.open("../prog1.dat");

    //If file can't be opened, exit
    if (!prog) {
        cerr << "File could not be opened" << endl;
        exit(EXIT_FAILURE);
    }
    else {
        while (!prog.eof()) {
            int size = 100, i, j;
            prog >> size; 
            int **numArray = new int* [size];
                for(i = 0; i < size; i++) {
                    numArray[i] = new int[size];
                    for(j = 0; j < size; j++) {
                        prog >> numArray[i][j];
                    }
                    cout << endl;
                }

                for(i = 0; i < size; i++) {
                    for(j = 0; j < size; j++) {
                         cout <<numArray[i][j] << " ";
                    }
                        cout << endl;
                }
        prog.close(); 
        return 0; 
        }
    }   
}

I'm extremely new to this programming language, this is actually my first program I've done in C++ and I'm literally learning as I go. Any suggestions would be greatly appreciated.

  • Well, as your first program better you find this out now rather than later. Doing this: `while (!prog.eof())` is [**nearly always wrong**](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). *Validate your inputs*. And as soon as you learn how dynamic allocation works, learn that you'll rarely need it if properly exploiting the standard library in modern c++. [**read this**](http://klmr.me/slides/modern-cpp/#1) – WhozCraig Sep 11 '14 at 11:24

1 Answers1

0

In this code You have not put a check on the difference with neighbour. Moreover their is no need for 2 nested for loops that is a very big overhead. You could have printed the numArray in the first nested for loop.

According to you it is your first programming assignment and you are already using double pointers and nested loops and also the way you checked the file is opened or not. Are you sure it's your first assignment

theadnangondal
  • 1,326
  • 3
  • 11
  • 27
  • Everything here are either things I've read in my textbook or different online tutorials, or from tips I've gotten from other people. It's definitely my first program. And by first programming assignment I mean in C++, I have taken a couple classes in Java and Visual Basic. –  Sep 11 '14 at 10:23