Can someone please convert this to C and solve the errors as well the errors as
ID: 3722350 • Letter: C
Question
Can someone please convert this to C and solve the errors as well the errors as listed below
using System;
#include <fstream>
#include <cstring>
#include <sstream>
#include <ctime>
#define RUNS 1000
using namespace std;
int main()
{
int child_count = 1;
string line;
stringstream ss;
time_t start,finish;
//Promp the user
cout << "Which file to use? ";
cin >> line;
cout << "How many child processes would you like to make [1, 2, 4] ";
cin >> child_count;
cout << "Computing Sum..." << endl;
char * cstr = new char [line.size()+1]; //Create C-String
strcpy (cstr, line.c_str()); //Copy line (file name) to the C-string
ifstream myfile (cstr); // Loads the file to read. Requires a C-String
int line_count = 0; // Number of Lines in the file or number count
int fds[child_count+1][2];//Stuff for the pipe.
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line); //Read Line
if(line != "")
{
line_count++; // increment line count
}
}
int numbers[line_count]; // Instantiate an array of numbers
ifstream myfile (cstr); // Re-Load the file to insert values into the array
int x = 0;
while ( myfile.good() )
{
getline (myfile,line);
if(line != "")
{
ss << line;
ss >> numbers[x];
x++;
}
}
start = time(NULL); // Start our timer for calculating run time.
// The loop for running to the program.
for(int n = 0; n < RUNS; n++) {
pipe(fds[child_count]); //We're using the last File Descriptor for Child -> Parent
for(int x = 0; x < child_count; x++)
{
pipe(fds[x]); // Instantiate the pipe
int indexes[2]; //This is an array of integers that determine the begginning and end of the Child's loop
indexes[0] = ((x)*(line_count/child_count)); //The beginning index
indexes[1] = ((x+1)*(line_count/child_count)); //The ending index
write(fds[x][1], &indexes, 2*sizeof(int)); //Send our array of indexes to child
if(fork() == 0) //If it is child
{
int temp = 0; //Temp variable for the sum
int indexes2[2];
read(fds[x][0], &indexes2, 2*sizeof(int)); //Read the index's from pipe
for(int k = indexes2[0]; k < indexes2[1] ; k++)
{
temp += numbers[k];
}
//Write the child's partion of the sum to the pipe
write(fds[child_count][1], &temp, sizeof(int));
_exit(0); //Exit the child process
}
}
int tmp, total;
total = 0;
for(int m =0; m < child_count; m++) {
read(fds[child_count][0], &tmp, sizeof(int));
total += tmp;
}
cout << "DONE Calculating!" << endl;
// cout << "TIME TOOK TO CALCULATE: " << time << " microseconds" << endl;
cout << "TOTAL OF NUMBERS: " << total << endl;
}
finish = time(NULL);
cout << "START " << start << " FINISH " << finish << endl;
float runtime = (float)((finish - start))/(RUNS);
cout << "AVERAGE RUN TIME: " << runtime << " seconds!" << endl;
cout << "EOP" << endl;
}
else
{
cout << "File Unable to Open!" << endl;
}
}
Please find this code also
int main()
{
int fd1[2]; // Used to store two ends of first pipe
int fd2[2]; // Used to store two ends of second pipe
char fixed_str[] = "forgeeks.org";
char input_str[100];
pid_t p;
if (pipe(fd1)==-1)
{
fprintf(stderr, "Pipe Failed" );
return 1;
}
if (pipe(fd2)==-1)
{
fprintf(stderr, "Pipe Failed" );
return 1;
}
scanf("%s", input_str);
p = fork();
if (p < 0)
{
fprintf(stderr, "fork Failed" );
return 1;
}
else if (p > 0)
{
char concat_str[100];
close(fd1[0]); // Close reading end of first pipe
write(fd1[1], input_str, strlen(input_str)+1);
close(fd1[1]);
wait(NULL);
close(fd2[1]); // Close writing end of second pipe
read(fd2[0], concat_str, 100);
printf("Concatenated string %s ", concat_str);
close(fd2[0]);
}
else
{
close(fd1[1]); // Close writing end of first pipe
char concat_str[100];
read(fd1[0], concat_str, 100);
int k = strlen(concat_str);
int i;
for (i=0; i<strlen(fixed_str); i++)
concat_str[k++] = fixed_str[i];
concat_str[k] = ''; // string ends with ''
close(fd1[0]);
close(fd2[0]);
write(fd2[1], concat_str, strlen(concat_str)+1);
close(fd2[1]);
exit(0);
}
}
[31m/temp/file.cs(4,0): error CS1024: Wrong preprocessor directive
/temp/file.cs(6,0): error CS1024: Wrong preprocessor directive
/temp/file.cs(8,0): error CS1024: Wrong preprocessor directive
/temp/file.cs(10,0): error CS1024: Wrong preprocessor directive
/temp/file.cs(12,0): error CS1032: Cannot define or undefine preprocessor symbols after first token in file
/temp/file.cs(13,6): error CS1525: Unexpected symbol `namespace', expecting `identifier' or `static'
/temp/file.cs(13,19): error CS1525: Unexpected symbol `;', expecting `identifier' or `static'
/temp/file.cs(19,0): error CS1525: Unexpected symbol `int', expecting `identifier' or `static'
/temp/file.cs(39,17): error CS1514: Unexpected symbol `new', expecting `.' or `{'
/temp/file.cs(39,15): error CS1530: Keyword `new' is not allowed on namespace elements
/temp/file.cs(39,18): error CS1525: Unexpected symbol `char', expecting `identifier' or `static'
[m
Compilation failed: 11 error(s), 0 warnings
Compilation Failed
Explanation / Answer
Hello There please note that your programs are throwing errors because of not including proper header files.
Include the below header files and remove the first statement "using system;"
#include <iostream> // for cin and cout
#include <unistd.h> // for pipe() and fork()
FinalCode.cpp
------------------------------------
#include <iostream> // for cin and cout
#include <unistd.h> // for pipe() and fork()
#include <fstream>
#include <cstring>
#include <sstream>
#include <ctime>
#define RUNS 1000
using namespace std;
int main()
{
int child_count = 1;
string line;
stringstream ss;
time_t start,finish;
//Promp the user
cout << "Which file to use? ";
cin >> line;
cout << "How many child processes would you like to make [1, 2, 4] ";
cin >> child_count;
cout << "Computing Sum..." << endl;
char * cstr = new char [line.size()+1]; //Create C-String
strcpy (cstr, line.c_str()); //Copy line (file name) to the C-string
ifstream myfile (cstr); // Loads the file to read. Requires a C-String
int line_count = 0; // Number of Lines in the file or number count
int fds[child_count+1][2];//Stuff for the pipe.
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line); //Read Line
if(line != "")
{
line_count++; // increment line count
}
}
int numbers[line_count]; // Instantiate an array of numbers
ifstream myfile (cstr); // Re-Load the file to insert values into the array
int x = 0;
while ( myfile.good() )
{
getline (myfile,line);
if(line != "")
{
ss << line;
ss >> numbers[x];
x++;
}
}
start = time(NULL); // Start our timer for calculating run time.
// The loop for running to the program.
for(int n = 0; n < RUNS; n++) {
pipe(fds[child_count]); //We're using the last File Descriptor for Child -> Parent
for(int x = 0; x < child_count; x++)
{
pipe(fds[x]); // Instantiate the pipe
int indexes[2]; //This is an array of integers that determine the begginning and end of the Child's loop
indexes[0] = ((x)*(line_count/child_count)); //The beginning index
indexes[1] = ((x+1)*(line_count/child_count)); //The ending index
write(fds[x][1], &indexes, 2*sizeof(int)); //Send our array of indexes to child
if(fork() == 0) //If it is child
{
int temp = 0; //Temp variable for the sum
int indexes2[2];
read(fds[x][0], &indexes2, 2*sizeof(int)); //Read the index's from pipe
for(int k = indexes2[0]; k < indexes2[1] ; k++)
{
temp += numbers[k];
}
//Write the child's partion of the sum to the pipe
write(fds[child_count][1], &temp, sizeof(int));
_exit(0); //Exit the child process
}
}
int tmp, total;
total = 0;
for(int m =0; m < child_count; m++) {
read(fds[child_count][0], &tmp, sizeof(int));
total += tmp;
}
cout << "DONE Calculating!" << endl;
// cout << "TIME TOOK TO CALCULATE: " << time << " microseconds" << endl;
cout << "TOTAL OF NUMBERS: " << total << endl;
}
finish = time(NULL);
cout << "START " << start << " FINISH " << finish << endl;
float runtime = (float)((finish - start))/(RUNS);
cout << "AVERAGE RUN TIME: " << runtime << " seconds!" << endl;
cout << "EOP" << endl;
}
else
{
cout << "File Unable to Open!" << endl;
}
}
---------------------------------------------------------
For the second program attached by you, use the below header files for proper compilation:
#include <unistd.h> // for pipe() and fork()
#include <stdio.h> // for printf() and scanf()
#include <string.h> // for string functions
#include <stdlib.h> // for exit()
#include <sys/wait.h> // for wait()
FinalCode.c
------------------------
#include <unistd.h> // for pipe() and fork()
#include <stdio.h> // for printf() and scanf()
#include <string.h> // for string functions
#include <stdlib.h> // for exit()
#include <sys/wait.h> // for wait()
int main()
{
int fd1[2]; // Used to store two ends of first pipe
int fd2[2]; // Used to store two ends of second pipe
char fixed_str[] = "forgeeks.org";
char input_str[100];
pid_t p;
if (pipe(fd1)==-1)
{
fprintf(stderr, "Pipe Failed" );
return 1;
}
if (pipe(fd2)==-1)
{
fprintf(stderr, "Pipe Failed" );
return 1;
}
scanf("%s", input_str);
p = fork();
if (p < 0)
{
fprintf(stderr, "fork Failed" );
return 1;
}
else if (p > 0)
{
char concat_str[100];
close(fd1[0]); // Close reading end of first pipe
write(fd1[1], input_str, strlen(input_str)+1);
close(fd1[1]);
wait(NULL);
close(fd2[1]); // Close writing end of second pipe
read(fd2[0], concat_str, 100);
printf("Concatenated string %s ", concat_str);
close(fd2[0]);
}
else
{
close(fd1[1]); // Close writing end of first pipe
char concat_str[100];
read(fd1[0], concat_str, 100);
int k = strlen(concat_str);
int i;
for (i=0; i<strlen(fixed_str); i++)
concat_str[k++] = fixed_str[i];
concat_str[k] = ''; // string ends with ''
close(fd1[0]);
close(fd2[0]);
write(fd2[1], concat_str, strlen(concat_str)+1);
close(fd2[1]);
exit(0);
}
}