Question

In: Computer Science

Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I -...

Complete the following program

#include<iostream>

#include<iomanip>

#include<fstream>

using namespace std;

int main()

{

// I - Declaring a five by five array

/* II - Read data from data.txt and use them to create

the matrix in the previous step*/   

// III - Count and print the number of even integers in the matrix.

/* IV - Calculate and print the sum of all integers in the columns with

an even index value. Please note the column index begins with zero.*/

/* V - Calculate the number of integers which are smaller than 20.*/

// Display of the matrix in a table format before transposing

// VI - Transpose the matrix

// Display of the matrix in a table format after transposing

return0;

}


//Thanks for the help ?

Solutions

Expert Solution

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

using namespace std;

int main() {
   // declaring variables
   int arr[5][5];
   int trans_arr[5][5];
   int data[25];
   int num;
   int k = 0;
   int even = 0;
   int sum_even_col = 0;
   int sum_all_even_col = 0;
   int less_twenty = 0;
   fstream file;


   // opening file 
   file.open("data.txt");


   // reading data into array
   while (file >> num) { 
       data[k] = num;
      k++;
   }


   // storing data into 5x5 array
   k = 0;
   for(int i = 0; i < 5; i++) {
      for(int j = 0; j < 5; j++) {
         arr[i][j] = data[k];
         k++;
      }
   }


   // counting number of even numbers
   for(int i = 0; i < 5; i++) {
      for(int j = 0; j < 5; j++) {
         if(arr[i][j] % 2 == 0) {
            even += 1;
         }
      }
   }
   cout << "Number of even numbers in the array: " << even << "\n" << endl;


   // calculating sum of all even column
   for(int i = 0; i < 5; i += 2) {
      for(int j = 0; j < 5; j++) {
         sum_even_col += arr[i][j];
      }
      sum_all_even_col += sum_even_col;
      cout << "Sum of the column " << i << " is: " << sum_even_col << endl;
   }
   cout << "Sum of all even columns is: " << sum_all_even_col << "\n" << endl;


   // count numbers less than 20
   for(int i = 0; i < 5; i++) {
      for(int j = 0; j < 5; j++) {
         if(arr[i][j] < 20) {
            less_twenty += 1;
         }
      }
   }
   cout << "Number of numbers in the array which are less than 20 are: " << less_twenty << "\n" << endl;


   // display matrix
   cout << setw(15) << "Matrix:" << endl;
   for(int i = 0; i < 5; i++) {
      for(int j = 0; j < 5; j++) {
         if(j == 4) {
            cout << arr[i][j];
            break;
         }
         cout  << arr[i][j] << setw(5);
      }
      cout << endl;
   }

   
   // transpose the matrix
   for(int i = 0; i < 5; i++) {
      for(int j = 0; j < 5; j++) {
         trans_arr[i][j] = arr[j][i];
      }
   }
   

   // display transpose
   cout << setw(20) << "\nTranspose Matrix:" << endl;
   for(int i = 0; i < 5; i++) {
      for(int j = 0; j < 5; j++) {
         if(j == 4) {
            cout << trans_arr[i][j];
            break;
         }
         cout  << trans_arr[i][j] << setw(5);
      }
      cout << endl;
   }
}

FOR HELP PLEASE COMMENT.
THANK YOU.


Related Solutions

complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char**...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char** argv) { int number, sum, count; // Write a while loop that reads a number from the user and loop // until the number is divisible by 7 cout << "What is the number? "; cin >> number; while ( ... ) { ... } cout << number << " is divisible by 7!! << endl << endl; // Write a for loop that...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count =...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;     cout << "Enter your student ID number: ";     cin >> studentid;     cout << "Student ID Number = " << studentid << endl;     while (studentid != 0)     {          numberreverse[count] = studentid % 10;          if (count == 0)          {              minimum = numberreverse[count];              maximum = minimum;          }          else...
#include <iostream> #include <iomanip> using namespace std; int main() {             float miles;   //miles traveled          &nbsp
#include <iostream> #include <iomanip> using namespace std; int main() {             float miles;   //miles traveled             float hours;   //time in hours             float milesPerHour; //calculated miles per hour             cout << "Please input the Miles traveled" << endl;             cin >> miles;             cout << "Please input the hours traveled" << endl;             cin >> hours;                         milesHours = miles / hours; cout << fixed << showpoint << setprecision(2);             cout << "Your speed is " <<...
9. #include <fstream> #include <iostream> using namespace std; int main() {     float bmi;     ifstream...
9. #include <fstream> #include <iostream> using namespace std; int main() {     float bmi;     ifstream inFile;     inFile.open("bmi.txt");     while (!inFile.eof())       {          inFile >> bmi;          if( bmi < 18.5)           {               cout << bmi << " is underweight " ;           }         else if( bmi >= 18.5 && bmi <= 24.9)           {               cout << bmi << " is in normal range " ;           }         else if( bmi >= 25.0 &&...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; struct Product {    string...
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; struct Product {    string itemname;    int id;    string itemcolor;    double cost; }; void fillProduct(Product[10], int& );//read data from a file and store in an array void writeProduct(Product[10], int);//write the array into a file void writeBinary(Product[10], int);//write the array into a file in binary mode void printbinary(Product[10], int);//read data from the binary file and print int main() {    Product myList[10];    int numItems = 0;...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
9. #include <fstream> #include <iostream> using namespace std; int main() { float bmi; ifstream inFile; inFile.open("bmi.txt");...
9. #include <fstream> #include <iostream> using namespace std; int main() { float bmi; ifstream inFile; inFile.open("bmi.txt"); while (!inFile.eof()) { inFile >> bmi; if( bmi < 18.5) { cout << bmi << " is underweight " ; } else if( bmi >= 18.5 && bmi <= 24.9) { cout << bmi << " is in normal range " ; } else if( bmi >= 25.0 && bmi <= 29.9) { cout << bmi << " is overweight " ; } else...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int SIZE = 20;     char str[SIZE];     char str1[SIZE];     int n;     int k =1;        printf("Enter a word: \n");     fgets(str,SIZE,stdin);     printf("Enter another word: \n");     fgets(str1,SIZE,stdin);        if (str1[strlen(str1) - 1] == '\n')     {         str1[strlen(str1)-1] = '\0';     }     if (str[strlen(str) - 1] == '\n')     {         str[strlen(str)-1] = '\0';     }      ...
Add File I/O to the voting program below #include<iostream> using namespace std; int main() {int choice;...
Add File I/O to the voting program below #include<iostream> using namespace std; int main() {int choice; int biden = 0 , trump = 0 , bugs = 0 ; int vc = 0 ; do { cout<<"\n\n\nEVOTE\n-----" <<"\n1.Joe Biden" <<"\n2.Donald Trump" <<"\n3.Bugs Bunny" // 4. Print current tally [hidden admin option] // 5. Print audit trail [hidden admin option] // 6. mess with the vote [hidden hacker option] E.C. // 7. END THE ELECTION <<"\n\n Your selection? "; cin >>...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT