Close Icon
Info
Warning
Danger
 / 
 / 
 / 
Question Description: Threesum. cpp program reads a file of numbers and forms

Question : Question Description: Threesum. cpp program reads a file of numbers and forms : 864516

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 + 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 ="<<start_t<<endl;< p="" style="box-sizing: border-box;"></start_t<<endl;<>

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<<"\nTotal Time:"<<total_t<<endl;< p="" style="box-sizing: border-box;"></total_t<<endl;<>

//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)<<endl;< p="" style="box-sizing: border-box;"></endl;<>

//Total pairs

cout<<"Total pairs:"<<count(a,n)<<endl;< p="" style="box-sizing: border-box;"></count(a,n)<<endl;<>

}

//otherwise

else

//Display message

cout<<"Cannot open file."<<endl;< p="" style="box-sizing: border-box;"></endl;<>

//stop

return 0;

}

Convert the program to standard C and Please I need (Full) written analysis and description of the program

 

Solution
5 (1 Ratings )

Solved
Computer Science 9 Months Ago 16 Views
This Question has Been Answered!

Unlimited Access

Explore More than 2 Million+
  • Textbook Solutions
  • Flashcards
  • Homework Answers
  • Documents
Signup for Instant Access!

Ask an Expert

Our Experts can answer your tough homework and study questions
154049 Computer Science Questions Answered!
Post a Question