Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Verizon LTE 9:25 PM @ 23% i), a learn.vccs.edu Program 8-Det Product The oejecti

ID: 3700683 • Letter: V

Question

Verizon LTE 9:25 PM @ 23% i), a learn.vccs.edu Program 8-Det Product The oejective ef this Program is t gain experience using ncil loeps and amays in a program by sealing oring and processing data from a ile Design a Cprogram to calculate the dot product of two amays The program should be gencral enough to process a difficnent mumber o data sets cach time it sun so the mumber of dat should process several scb sets to be peecessed is an ntry in the file. For each dana set, yourpgram should do the following l) Use a loop structure to readthe data from a file and sore #intwo different amy. Onoe all the data has boen read in, use a second loop to find the dot product of the two amays For example, the doe peodact of two amays, x and y, with 3 cloments cach would be )Prim out the anray values, with two decimal places, side by side, including the element mumber and then print out the dat product value Label the information clearly For cumple, a data set coiting o2 amays of 4 clemts cach could be amangod in a file like this 20 85 The printout for this data set would look something like the Sollowing Elemest Array Armay 2 .0 The det product of the twe arrays is 23. You should not need more than two arrays. Process each data set bedore you read in the next dat set You may assune there are never more d an 50 elements in any aray. You may choose to nclude function"n the program, b- s not a roqairemnt Test your program with the datafile ProgRaa Ex available in Blackboand The data file contats 5 lines eftest athe beginng describnghow the file is so' up. ead, and print these lises of Please do mot aher the contenes or arrangemeat of this fie Use standard programming practices, iscluding ood uss of variable narmes and good commens EGR 126 This is a cepy of the data file provided for you in Blackhoard:

Explanation / Answer

ScreenShot

------------------------------------------------------------------------------------------------------------------

program

//header file
#include <iostream>
#include <iomanip>
#include <fstream>
#include<vector>
using namespace std;
//Main method
int main() {
   //variable declaration
   int count,count1,array[1],array1[1];
   int j = 0;
   int dotP = 0;
   //file object creation
   ifstream inFile;       
   //Open fie for reading
   inFile.open("C:/Users/deept/Desktop/dotproduct.txt");
   //check the file can open or not
   if (!inFile) {
       cout << "Unable to open file";
       exit(1); // terminate with error
   }
   //first line take as count
   inFile >> count;
   //To fill the second array
   count1 = 2 * count;
   //array size initialization
   array[count];
   array1[count1];
   //loop through the file to reach end
   do {
       //first array fill
       for (int i = 0; i < count; i++) {
           inFile >> array[i];
       }
       //second array fill
       for (int i = count; i < count1; i++) {
            inFile >> array1[j];
           j++;
       }
       //find dot product
       for (int i = 0; i < count; i++) {
           dotP += array[i] * array1[i];
       }
       //display the result
       cout << "Element         Array1             Array2 ";
       for (int i = 0; i<count; i++) {
           cout << " " << i << "                " << array[i] << "                  " << array1[i] << ' ';
       }

       cout << " Dot Product of the arrays =" << dotP << endl;
       cin.get(); //Keep program open until "enter" is pressed

   } while (!inFile.eof());//Check the end of file
   inFile.close();//Close file
    return 0;
}