Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

pleae solve this programming problem , using C Programming ,and not C++ or Java.

ID: 3716496 • Letter: P

Question

pleae solve this programming problem , using C Programming ,and not C++ or Java.

During this Spring break, Sam had enough time to make some nice tour in the UAE. He had spent total of 8 days in visiting Dubai, Ras Al-khaimah and Fujairah. His cost per day in Dubai was 750 AED, while Ras Al-khaimah daily cost was 550 AED, and he spent 650 AED for each day in Fujairah, with grand total of 5400 AED. The number of days he spent in Dubai was the same as the total number of days in both Ras Al-khaimah and Fujairah. Eventually, when he came back, he asked you to write a program to find out how many days did he spend in each city during his tour?

This problem can be represented and modelled by set of linear equations. For example: Assume:

the number of days spent in Dubai is X.

the number of days spent in Ras Al-khaimah is Y.

the number of days spent in Fujairah is Z.

Then, the statement “He had spent total of 8 days in visiting Dubai, Ras Al-khaimah and Fujairah”, can be modelled as:

X + Y + Z = 8

And so forth, then you will end up with system of linear equations; particularly:

X + Y + Z = 8

X - Y - Z = 0

750X + 550Y + 650Z = 5400

Your target now should to solve this system of linear equations using Gaussian Elimination technique .

Explanation / Answer

/* I have written the C code below for this specific problem. Compiled and tested it

answer is X=4,Y=2,Z=2 and the program shows the same. Wherever required i Have commented appropriately.*/

#include <stdio.h>
int main(void)
{
int n; // n denotes order of the matrix
// to take input from user
// scanf("%d",&n);
// for this specific question n=3
n=3;
double A[n][n+1];
/*This is the augmented matrix written row wise

there are n+1 columns

the eqns are actually written this way
X+Y+Z=8
X-Y-Z=0
750X+550Y+650Z=5400
the matrix is
1 1 1 8
1 -1 -1 0
750 550 650 5400

so 3 rows and 4 columns
in general n rows and n+1 columns
*/
/*
for(int i=0;i<n;i++)
{
for(int j=0;j<n+1;j++)
scanf("%f",&A[i][j]);
}
but in this question values are already mentioned in question
*/
A[0][0]=1;
A[0][1]=1;
A[0][2]=1;
A[0][3]=8;
A[1][0]=1;
A[1][1]=-1;
A[1][2]=-1;
A[1][3]=0;
A[2][0]=750;
A[2][1]=550;
A[2][2]=650;
A[2][3]=5400;
/*Now generate upper traingular matrix*/
int i,j,k;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i>j)
{
double temp=A[i][j]/A[j][j];
for(k=0;k<n+1;k++)
A[i][k]=A[i][k]-temp*A[j][k];
}
}
}
// answer ans[0] is X
// answer ans[1] is Y
// answer ans[2] is Z
double ans[n];
ans[n-1]=A[n-1][n]/A[n-1][n-1];

//backward subsitution loop
for(i=n-2;i>=0;i--)
{
double sum=0.0;
for(j=i+1;j<n;j++)
sum+=(A[i][j]*ans[j]);
ans[i]=(A[i][n]-sum)/A[i][i];
}

//SOLUTION
printf("X=%f ",ans[0]);
printf("Y=%f ",ans[1]);
printf("Z=%f ",ans[2]);
return 0;
}