Answer to Question #24468 in C for gene

Question #24468
Write a c program that lets the user input something like the example below only using scanf():
c 1
c 2
c 3
c 4
a 1 2 1900
a 1 3 1950
a 1 4 1400
a 2 3 150
a 2 4 3100
a 3 4 3150

And after the user is finished use printf() to output the following sentence example:
The tour 2 1 3 4 2 costs 10100.
1
Expert's answer
2013-02-19T09:03:07-0500
#include <stdio.h>int main(){
intadj[100][100];
for(inti = 0; i < 100; i++){
for(intj = 0; j < 100; j++){
adj[i][j]= -1;
}
}
charch;
intd;
while(true){
scanf("%c",&ch);
if(ch== 'a')
break;
scanf("%d",&d);
getchar();
}
inti, j, distance;
scanf("%d%d %d", &i, &j, &distance);
getchar();
adj[i-1][j-1]= adj[j-1][i-1] = distance;
for(intk = 1; k < d*(d - 1)/2; k ++){
scanf("%c%d %d %d", &ch, &i, &j, &distance);
getchar();
adj[i-1][j-1]= adj[j-1][i-1] = distance;
}
inttour[] = {2, 1, 3, 4, 2};
intprice = 0;
printf("Thetour ");
printf("%d", tour[0]);
for(inti = 0; i < 4; i ++){
price+= adj[tour[i]-1][tour[i+1]-1];
printf("%d", tour[i+1]);
}
printf("costs%d.", price);
getchar();
return0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

gene
26.02.13, 04:13

This program does not work

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS