Hello, I am struggling with a C language program. Can you help me? My program(co
ID: 3884764 • Letter: H
Question
Hello, I am struggling with a C language program. Can you help me?
My program(code included below) already takes an input file(a flowshop data file), creates an array and stores the flowshop information. I need to add some things to it but i dont know how and was hoping you could show me.
These are the things im having problems with:
randomly initialize a population(example: 100)
compute the makespans of the population
select the best schedule according to makespans
output the schedule and its makespan to the output file
Can you help me here? i have no idea what to do with a makespan or a schedule or how to make a population . Thank you for the help.
I have included examples of the input file and what the output file should look like.
Here is an example of a data input file
___________________
//input file
3 2
0 5 1 10
0 10 1 5
0 1 1 4
____________________
in the input file, the first row indicates the number of jobs and the number of machines, after that each row represents a specific job combined by (machine-id,job-duration) pairs.
____________________
//output file
makespan
21
2 0 0 1 1 6
2 1 0 6 1 16
_____________________
in the output file, the first line is the optimization criterion, the second line is the makespan computer from the schedule. The rest is the flowshop schedule combined by (job-id, start-time) pairs.
My program:
________________________
#include<stdlib.h>
#include<stdio.h>
int main()
{
FILE *fp;
int m,n,i,j;
int **arr;
int dim[2];
char file_name[20];
//reading file name from user
printf("Enter file Name :");
scanf("%s", file_name);
fp = fopen(file_name, "r"); //opening file in read mode
if(fp == NULL) //if file is not present in working dir
{
printf(" Cannot open file ");
exit(0);
}
//reading dimension of matrix
for(i = 0; i < 2; i++)
{
if(!fscanf(fp, "%d", &dim[i]))
break;
}
m = dim[0];
n = 2 * dim[1]; //for column as column is 2 * given value
arr = (int **)malloc(m * sizeof(int *)); //creating 2-D array for reading data from file
for (i=0; i < m; i++)
arr[i] = (int *)malloc(n * sizeof(int));
//reading all data except first line in 2-D array
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
if(!fscanf(fp, "%d ", &arr[i][j]))
break;
}
}
//printing array by alternate column wise
for(i = 1; i < n; i += 2)
{
for(j = 0; j < m; j++)
{
printf("%d ", arr[j][i]);
}
printf(" ");
}
for (i = 0; i < m; i++)
{
free(arr[i]);
}
free(arr);
fclose(fp); //closing file.
return 0;
}
__________________________
Explanation / Answer
#include<stdlib.h>
#include<stdio.h>
int main()
{
FILE *fp;
int m,n,i,j;
int **arr;
int dim[2];
char file_name[20];
//reading file name from user
printf("Enter file Name :");
scanf("%s", file_name);
fp = fopen(file_name, "r"); //opening file in read mode
if(fp == NULL) //if file is not present in working dir
{
printf(" Cannot open file ");
exit(0);
}
//reading dimension of matrix
for(i = 0; i < 2; i++)
{
if(!fscanf(fp, "%d", &dim[i]))
break;
}
m = dim[0];
n = 2 * dim[1]; //for column as column is 2 * given value
arr = (int **)malloc(m * sizeof(int *)); //creating 2-D array for reading data from file
for (i=0; i < m; i++)
arr[i] = (int *)malloc(n * sizeof(int));
//reading all data except first line in 2-D array
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
if(!fscanf(fp, "%d ", &arr[i][j]))
break;
}
}
//printing array by alternate column wise
for(i = 1; i < n; i += 2)
{
for(j = 0; j < m; j++)
{
printf("%d ", arr[j][i]);
}
printf(" ");
}
for (i = 0; i < m; i++)
{
free(arr[i]);
}
free(arr);
fclose(fp); //closing file.
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.