5. Consider a data file named STORES.DAT, which contains inventory information for four shops. Each line in the
file contains the inventory of five types of merchandize in each store. Write a program to perform the following:
(a) Read the data into a two-dimensional array invent(4,5).
(b) Produce the output shown below, which includes:
- finding the total inventory of all items in each store
- finding the maximum quantity of each item
THE OUTPUT:
ITEMS
,#1 #2 #3 #4 #5 TOTAL
- - - - - - - - - - - - - - - - -
STORE#1 14 0 5 2 16 37
STORE#2 15 20 0 0 5 40
STORE#3 25 0 10 30 20 85
STORE#4 5 3 3 6 0 17
- - - - - - - - - - - - - - - - -
Max Quant 25 20 10 30 20

and all i could get is this i know its wrong but no idea how to make it go right !

# include <iostream>
using namespace std;
int main ()
{
    int a[5][6],n=1,sum,max=0,i=0 ,j=0;

    for( i ; i<4 ; i++){
        sum=0;

    cout<<"STORE#"<<n;

    n++;
    for( j ; j<5 ; j++){
  cin>>a[i][j];
   sum+=a[i][j];

    }
         cout<<sum; 
    }
          for( j=0 ; j<5 ; j++)
        for(i=0 ; i<4 ; i++){

            a[i][j]=max;
            if (a[i][j]<max)
            max=a[i][j];

        cout<<max;


    }        


return 0;
}

Recommended Answers

All 3 Replies

Besides your horrible indentation(you definitly work on that) the code at first sight, looks quite OK. I think you just need to use an endline now and then.
cout << "a line" << endl

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

int main()
{
    int Max[5];
    int sum[4] = {0,0,0,0};
    ifstream fin;
    fin.open("STORES.txt");
    int items[4][5];

    for(int x=0;x<4;x++)
        for(int y=0;y<5;y++)
        {
            fin>>items[x][y];
            Max[y] = items[0][y];
            sum[x] = sum[x] + items[x][y];
        }

    cout<<"               #1 \t#2 \t#3 \t#4 \t#5 \tTotal"<<endl;
    cout<<"---------------------------------------------------------------"<<endl;
    for(int x=0;x<4;x++)
    {
        cout<<"Store #"<<x+1<<"       ";
        for(int y=0;y<5;y++)
        {
            cout<<items[x][y]<<"\t";
        }
        cout<<sum[x]<<endl;
    }
    cout<<"---------------------------------------------------------------"<<endl;
    cout<<"Max Quant"<<"\t";
    for(int x=0;x<4;x++)
        for(int y=0;y<5;y++)
            if(Max[y] < items[x][y])
                Max[y] = items[x][y];
    for(int x=0;x<5;x++)
        cout<<Max[x]<<"\t";
    cout<<endl<<endl;
    fin.close();
    system("pause");
    return 0;
}

so i wrote it finally and it runs perfectly but with diff values !!! anyone knows why or what to do ???

The data file example you provided above ... looks defective?

... Consider a data file named STORES.DAT, which contains inventory information for four shops. Each line in the file contains the inventory of five types of merchandize in each store ...

So ... the file has 4 lines.
BUT ... EACH line should have only 5 numbers (integers)!

Also, if you want portable code, do not use Windows system calls ...

Also, good to break up jobs into functions.
This also helps one to 'see' more easily the logic flow in the program.

You may like to see the example below:

// compareStores.cpp //

#include <iostream>
#include <iomanip> // re. setw
#include <fstream>
#include <string>


const char* FNAME = "stores.dat";
/*
 1 14  0  5  2
15 20  0  0  5
25  0 10 30 20
 4  5  3  3  6
*/

const int ROWS = 4, COLS = 5;


bool fillFromFile( int mat[][COLS], int aryMins[], int aryMaxs[], int totals[],
                   const char* inFileName )
{
    std::ifstream fin( inFileName );
    if( fin )
    {
        // read file into matrix ...
        for( int row = 0; row < ROWS; ++ row )
        {
            for( int col = 0; col < COLS; ++ col )
            {
                fin >> mat[row][col];
                if( col == 0 )
                    aryMins[row] = aryMaxs[row] = col;
                else
                {
                    if( mat[row][col] < mat[row][aryMins[row]] )
                        aryMins[row] = col;
                    else if( mat[row][col] > mat[row][aryMaxs[row]] )
                        aryMaxs[row] = col;
                }

                totals[row] += mat[row][col];
            }
        }
        fin.close();
        return true;
    }
    // if reach here ...
    std::cout << "\nThere was a problem opening file "
              << inFileName << '\n';
    return false;
}

void showTable( const int mat[][COLS], const int totals[] )
{
    using std::cout; using std::endl; using std::setw;
    using std::string;

    // print header line ...
    cout << string( 8, ' ' );
    for( int i = 1; i <= COLS; ++ i ) cout << setw(7) << '#' << i;
    cout << setw(8) << "Sums" << endl;

    cout << string( 56, '-' ) << endl;

    // print data  for each row (each Store) ...
    for( int r = 0; r < ROWS ; ++ r )
    {
        cout << "Store #" << r+1;
        for( int c = 0; c < COLS; ++ c )
             cout << setw(8) << mat[r][c] ;
        cout << setw(8) << totals[r] << endl;
    }

    cout << string( 56, '-' )  << endl;
}

void showExtremes( const int mat[][COLS], const int aryCols[] )
{
    using std::cout; using std::setw;

    for( int r = 0; r < ROWS; ++ r )
        cout << setw(8) << mat[r][aryCols[r]];
}




int main()
{
    using namespace std;

    int stores[ROWS][COLS];

    int colsHoldingMin[ROWS];
    int colsHoldingMax[ROWS];

    int sums[ROWS] = { 0 };

    if( fillFromFile( stores, colsHoldingMin, colsHoldingMax, sums, FNAME ) )
    {
        showTable( stores, sums );

        cout<<"MinInRow";
        showExtremes( stores, colsHoldingMin );
        cout << endl;

        cout<<"MaxInRow";
        showExtremes( stores, colsHoldingMax );
        cout << endl;
    }

    cout << "\nPress 'Enter' to continue/exit ... " << flush;
    cin.get();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.