Description: Threesum. cpp program reads a file of numbers and forms a sequence
ID: 3820218 • Letter: D
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<iostream>
#include<string>
#include<fstream>
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;
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])<<" ";
//Count the pair
counting++;
}
//end of clock
end_t = clock();
//Display total running time..
total_t = (double)(end_t - start_t);
cout<<" Total Time:"<<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;
//Total pairs
cout<<"Total pairs:"<<count(a,N)<<endl;
}
//otherwise
else
//Display message
cout<<"Cannot open file."<<endl;
//stop
return 0;
}
Convert the program to standard C and Please I need (Full) written analysis and description of the program
Explanation / Answer
C code:
#include <stdio.h>
#include <stdlib.h>
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;
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
printf("%i ", (a[i]+a[j]+a[k]));
// cout<<(a[i]+a[j]+a[k])<<" ";
//Count the pair
counting++;
}
//end of clock
end_t = clock();
//Display total running time..
total_t = (double)(end_t - start_t);
printf(" Total Time:%i ",total_t);
return counting;
}
//Starting of main function
int main()
{
//Declare file pointer
FILE *myFile;
//Declare array a of type int
int a[10000];
//Declare N
int N=0;
//Open the file
myFile = fopen("input.txt", "r");
//Read integer
while(!feof(myFile))
{
fscanf(myFile, "%d", &a[N]);
N++;
}
//Display the number count
printf("%s %i ", "Numbers in the file:", N);
//Total pairs
int out = count(a,N);
printf("%s %i ", "Total pairs:", out);
return 0;
}
Sample input.txt:
11 22 33 44 55 6 7 8 9 10
Sample Output:
Numbers in the file: 10
66 77 88 39 40 41 42 43 33 88 99 50 51 52 53 54 44 110 61 62 63 64 65 55 72 73 74 75 76 66 24 25 26 27 17 26 27 28 18 28 29 19 30 20 21 99 110 61 62 63 64 65 55 121 72 73 74 75 76 66 83 84 85 86 87 77 35 36 37 38 28 37 38 39 29 39 40 30 41 31 32 132 83 84 85 86 87 77 94 95 96 97 98 88 46 47 48 49 39 48 49 50 40 50 51 41 52 42 43 105 106 107 108 109 99 57 58 59 60 50 59 60 61 51 61 62 52 63 53 54 68 69 70 71 61 70 71 72 62 72 73 63 74 64 65 21 22 23 13 23 24 14 25 15 16 24 25 15 26 16 17 27 17 18 19
Total Time:20
Total pairs: 165