Hello, I am struggling with a C language program. Can you help me? the program i
ID: 3884473 • Letter: H
Question
Hello, I am struggling with a C language program. Can you help me?
the program is supposed to initialize a population of numbers, compute the makespan, and output the schedule.
Here is an example of a data 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.
I have a program already made that will read an input file with a flowshop schedule, then sort and store it in an array. I have included that below:
________________________
#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;
}
__________________________
These are the things im having problems with:
should read the output filename(the second argument should be the output file name)
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.
Explanation / Answer
#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.