Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- #include <ctime>
- #include <cstdlib>
- #include <iomanip>
- #include <fstream>
- using namespace std;
- int randgen(int magnitude);
- int subtract(int, int deduction);
- void populateArray(int a[][20],int,int);
- void getTotal(int a[][20],int,int,int&);
- void getAverage(int a[][20],int,int,float&, int);
- int getRowTotal(int a[][20],int,int,int);
- int getColumnTotal(int a[][20],int,int,int);
- int getHighestInRow(int table[][20],int,int);
- int getLowestInRow(int table[][20],int,int);
- void fout(ofstream&, int table[][20],int, int, int, float); //This function is HUGE; it takes care of practically all the output operations besides the pretty bars
- void pretty(int, ofstream &file); //my previous professor, prof. Ahren, said it is good to personalize our work (this is a generalization). If you're going to mark me down for this please don't and tell me not to do things like this in the future?
- int main()
- {
- srand(time(NULL));
- ofstream file;
- file.open("out.txt");
- int columns=randgen(16)+5, rows=randgen(16)+5, table[20][20]={0}, total; //awwwww yeah look at that functions as initializers
- float average;
- populateArray(table,rows,columns);
- getTotal(table,rows,columns,total);
- getAverage(table,rows,columns,average, total);
- fout(file, table, rows, columns, total, average);
- return 0;
- }
- void fout(ofstream &file, int table[][20],int rows, int columns, int total, float average)
- {
- file << setw(2) << left << rows << " " << "rows\n";
- file << setw(2) << left << columns << " "<< "columns\n";
- for(int i=0;i<rows;i++){
- if(i==0)
- pretty(columns,file);
- for(int k=0;k<columns;k++){
- file << setw(3) << right << table[i][k] << " ";
- if(k==columns-1)
- file << endl;
- }
- if(i==rows-1)
- pretty(columns,file);
- }
- file << endl;
- for(int index=0;index<rows;index++){
- file << "Row " << setw(2) << index+1 << " Total: " << right << setw(4) << getRowTotal(table,columns,rows,index);
- file << setw(9)<< " Highest: " << setw(3) << getHighestInRow(table, index, columns);
- file << setw(9)<< " Lowest: " << setw(3) << getLowestInRow(table, index, columns);
- file << endl;
- }
- file << endl;
- for(int indexx=0;indexx<columns;indexx++)
- file << setw(7) << "Column " << setw(2) << indexx+1 << setw(7) << " Total: " << setw(4) << getColumnTotal(table,columns,rows,indexx) << endl;
- file << endl;
- file << "The total of all " << columns*rows << " numbers is: " << total <<". ";
- file << "The average is " << average;
- pretty(columns,file);
- return;
- }
- void pretty(int columns, ofstream &file)
- {
- static int greeting=0;
- if (greeting!=2){
- for(int pretty=0;pretty<columns*4;pretty++)
- file << "=";
- file << endl;
- }
- else if (greeting==2)
- cout << "Look in the same folder that HOMEWORK2 is saved in. You will\nfind a text file named out.txt; open it up!\n(You can close this window now)\n";
- greeting++;
- }
- int getLowestInRow(int table[][20], int row, int columns)
- {
- int lowest=666;
- for(int index=0;index<columns;index++)
- if(table[row][index]<lowest)
- lowest=table[row][index];
- return lowest;
- }
- int getHighestInRow(int table[][20], int row, int columns)
- {
- int highest=0;
- for(int index=0;index<columns;index++)
- if(table[row][index]>highest)
- highest=table[row][index];
- return highest;
- }
- int getColumnTotal(int table[][20], int columns, int rows, int column)
- {
- int columnTotal=0;
- for(int index=0;index<rows; index++)
- columnTotal+=table[index][column];
- return columnTotal;
- }
- int getRowTotal(int table[][20], int columns, int rows, int row)
- {
- int rowTotal=0;
- for(int index=0;index<columns; index++)
- rowTotal+=table[row][index];
- return rowTotal;
- }
- void getAverage(int table[][20],int rows, int columns, float &average, int total)
- {
- average=static_cast<float>(total)/(rows*columns);
- return;
- }
- void getTotal(int table[][20], int rows, int columns, int &total)
- {
- total=0;
- for(int i=0;i<rows;i++)
- for(int k=0;k<columns;k++){
- total+=table[i][k];
- }
- return;
- }
- int randgen(int magnitude)
- {
- return rand()%magnitude;
- }
- int subtract(int number, int deduction)
- {
- if(rand()%2==0)
- return number-deduction;
- else
- return number;
- }
- void populateArray(int a[][20],int rows,int columns)
- {
- for(int i=0;i<rows;i++)
- for(int k=0;k<columns;k++)
- a[i][k]=subtract(randgen(26),25);
- }
Advertisement
Add Comment
Please, Sign In to add comment