Question: Description: Threesum. cpp program reads a file of numbers and forms a sequence of sums of three numbers in the following manner: (1st + 2nd
Description:
Threesum. cpp program reads a file of numbers and forms a sequence of sums of three numbers in the following manner: (1st + 2nd + 3rd), (1st + 2nd + 4th), ...,(1st + 2nd + nth), (1st + 3rd + 4th), ...,(1st + 3rd + nth), ...,(1st + (n-1)th + nth), (2nd + 3rd + 4th), ...,((n-2)th + (n-1)th + nth). It can be shown that this is an O(n3) algorithm, and hence a slower process.
Program:
//Include the header files
#include
#include
#include
using namespace std;
//A function Definition
int count(int *a,int N)
{
//Clock varibales
clock_t start_t, end_t, total_t;
int i,j, k;
//Starting the clock
start_t = clock();
cout<<"Starting of the program, start_t ="< int counting = 0; //Use three loops to find the sum of three for (i = 0; i <= N; ++i) for (j = i+1; j <= N; ++j) for (k = j+1; k <= N; ++k) { //Display the sum cout<<(a[i]+a[j]+a[k])<<"\t"; //Count the pair counting++; } //end of clock end_t = clock(); //Display total running time.. total_t = (double)(end_t - start_t); cout<<" Total Time:"< //Return the pair return counting; } //Starting of main function int main() { //Declare file pointer ifstream fid; //Declare array a of type int int a[10000]; //Declare N int N=0; //Open the file fid.open("numbers1.dat"); //Check if file is open if(fid.is_open()) { //Read integer while(fid>>a[N]) { //Increment the N by 1 N++; } //Close the file fid.close(); //Display the number count cout<<"Numbers in the file:"<<(N+1)< //Total pairs cout<<"Total pairs:"< } //otherwise else //Display message cout<<"Cannot open file."< //stop return 0; } Convert the program to standard C and Please I need (Full) written analysis and description of the program
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
