please help me with this c project Objective: To be able to write a C program th
ID: 3692384 • Letter: P
Question
please help me with this c project
Objective:
To be able to write a C program that solves problems found in Engineering.
To be able to apply common C constructs such as: Functions, loops, arrays and file processing functions.
To design a project that focuses on problem solving, rather than writing a complete application or good interfaces.
To design a project of various difficulty to expose and encourage talented students.
To design a project with few details to encourage creativity in how to solve the problem and to state assumptions.
Deliverables:
Source code in C file. No .exe file should be
attached.
Synopsis: Programming techniques can solve many problems in the engineering domain. Here are three types of engineering problems that can be solved automatically by Programming:
Problems that describes the relations between more than one variable in equation-form such as: Basic Algebra equations, Geometric functions and Calculus. We have covered many examples of these in the course such as: The area of circle, the sine function, the third side length in right angle triangle.
Problems that require inference about variable values such as: Maximum, Minimum, Sum, Average, Transpose, the inversion..etc. We partially covered this in the course using repetition to find the maximum in one dimension data.
Advanced problems that require designing algorithms in order to solve it. These algorithms are either: exact algorithms or stochastic algorithms. These problems are referred to as: optimization and approximation problems. This has not been covered in the course.
Problem Statement:
You have to generate a table of 16 rows and 4 columns and apply some processings on it. Different levels of difficulty is designed below from easy to moderately difficult tasks:
Table 1: Example Data
C1
C2
C3
C4
R1
5
1
0
1
R2
1
2
0
3
R3
2
1
0
4
R4
1.5
0
2.25
0
R5
3
0
0
0
R6
-2
1
1
0
R7
1
1
3
-1
…
…
..
..
…
…
…
..
..
…
R16
…
..
..
…
Level 1 (5 Marks):
Use a random function for populating the data in the table (filling up the table cells) and write them to a text file named input.txt. Don’t write the table value yourself. Set the range of data in each cell to include positive and negative floating numbers.
Read the data from a data file called input.txt and print the results a data file called output.txt
Random function returns integer only. How you can make it return negative or floating number?
Level 2 (15 Marks):
In your generated Table:
Find the first maximum value, first minimum value, sum and average of each row using repetition.
Find the first maximum value, first minimum value, sum and average of each column for all columns using repetition.
Write a separate function for the maximum, the minimum, the sum and the average. The function accepts two parameters: row’s number and column’s number. In total, you must have
four functions in addition to the main. These functions are supposed not to have scanf or printf statements.
You may do the processing in the file, you may use array…etc . Decide which technique is easy for you?
Topics Covered: Function, Selection, Repetitions
Level 3 (15 Marks):
Find the position of the last maximum and the last minimum in each row. Similarly, find the position of the last maximum and the last minimum in each column.
Searching the whole array, find the position of the maximum value and the minimum value of the whole array. If there is more than one maximum or minimum, return all of them.
You may use functions here, but it is not necessary as in the Level 2. You decide what is the best approach to tackle the problem here.
Topics Covered: Pointers, Array and 2D Array
Level 4 (20 Marks):
Assume that each 4 rows belong to one column.
For Example, Rows 1-4 belong to C1,
Rows 5-8 belong to C2,
Rows 9-12 belong to C3,
Rows 13-16 Belong to C4.
If the maximum of each row is not under its correspondent column, swap the cell values.
For example, in the table, it’s clear, that R2’s maximum is in C4, so we recommend swap the values. So R2C1 value is 3 and R2C4 value is 1.
Following the previous assumption, now you have to create a one-to-one correspondence between rows and columns. That means, each row must have only one value which is the maximum.
If a row has more than one value, all other values must be copied to another array called Auxiliary Array.
For example, in R1, there are three values: 5 belongs to C1, 1 belongs to C2 and 1 belongs to C4. So, applying the requirement of this question, 5 is the maximum and it will stay as it is since Rows 1-4 belong to C1.
We will copy the value of R1C2 into the auxiliary array in the same index. Similarly, we will copy the value of R1C4 into the auxiliary array at the same index.
Bonus:
Instead of applying Level 4 on all rows using loops which is quite easy. Here, a random number is generated using random function to pick a number form the cells 1-64 (the number of data in a table of 16 rows and 4 columns).
For example, the random function might return the number 21. 21 corresponds to (R6, C1) cell. The maximum of this row is 2 under C1 column. But R6 belongs to C2, so the maximum must belong to C2. So, as we did in Level 4, we swap the maximum to the right position and we copy the non-maximum values in that row only to the
C1
C2
C3
C4
R1
5
1
0
1
R2
1
2
0
3
R3
2
1
0
4
R4
1.5
0
2.25
0
R5
3
0
0
0
R6
-2
1
1
0
R7
1
1
3
-1
…
…
..
..
…
…
…
..
..
…
R16
…
..
..
…
Explanation / Answer
#include <stdio.h>
float table[16][4];
int i,j;
void sum(int row, int column)
{
int sum=0;
if(row=!-1)
{
for(i=0;i<4;i++)
{
sum+=table[row][i];
}
printf(" %f Sum :",sum);
}
if(cloumn!=-1)
{
for(i=0;i<16;i++)
{
sum+=table[row][column];
}
printf(" %f Sum:",sum);
}
}
int average(int row, int column)
{
int sum=0;
if(row=!-1)
{
for(i=0;i<4;i++)
{
sum+=table[row][i];
}
printf(" %f Average:",sum/4);
}
if(cloumn!=-1)
{
for(i=0;i<16;i++)
{
sum+=table[i][column];
}
printf(" %f Average:",sum/16);
}
}
int firstMinimum(int row, int column)
{
int min;
if(row=!-1)
{
min=table[row][0];
for(i=1;i<4;i++)
{
if(table[row][i]<min)
{
min=table[row][i];
break;
}
}
printf(" %f First minimum:",min);
}
if(cloumn!=-1)
{
min=table[0][column];
for(i=0;i<16;i++)
{
if(table[i][column]<min)
{
min=table[row][i];
break;
}
}
printf(" %f First minimum:",min);
}
}
int firstMaximum(int row, int column)
{
int max;
if(row=!-1)
{
max=table[row][0];
for(i=1;i<4;i++)
{
if(table[row][i]>max)
{
max=table[row][i];
break;
}
}
printf(" %f First maximum:",max);
}
if(cloumn!=-1)
{
max=table[0][column];
for(i=0;i<16;i++)
{
if(table[i][column]>max)
{
max=table[row][i];
break;
}
}
printf(" %f First maximum :",max);
}
}
int main()
{
FILE *fp,*fp1;
fp = fopen("input.txt", "w+");
int max=10;
int min=-10;
float value;
/*fill the table with random positive or negative floating point numbers */
/* write the numbers into input.txt*/
for(i=0;i<16;i++)
{
for(j=0;j<4;j++)
{
value=float(rand()) / float(RAND_MAX)) * (Max - Min)) + Min;
table[i][j]=value;
fprintf(fp,"%f "value);
}
}
fclose(fp);
//open input.txt in read mode
fp = fopen("input.txt", "r");
//open output.txt in write mode
fp1=fopen("output.txt","w+");
//read from input.txt and write into output.txt
while(!feof(fp))
{
fscanf(fp,"%f",&value);
fprintf(fp1,"%f ",value);
}
fclose(fp);
fclose(fp1);
//each row
for(i=0;i<16;i++)
{
sum(i,-1);
average(i,-1);
firstMinimum(i,-1);
firstMaximum(i,-1);
}
//each column
for(i=0;i<4;i++)
{
sum(-1,i);
average(-1,i);
firstMinimum(-1,i);
firstMaximum(-1,i);
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.