subject

THE FOLLOWING IS WRITTEN IN C: Summary: This assignment explores the use of basic conditional execution, nested iteration, and
memory accesses to analyze time/space data such as information collected by GPS-enabled
applications In particular, you will write a program that analyzes two timelines, each of which
gives a history of major cities in which a person has lived in chronological order. Your program
will determine the earliest year in which both people lived in the same city.
Data in each timeline TL is stored as a sparse vector of ten elements in the following format (for
i=0, 1, …, 4):
TL[i*2] = Duration: number of years a person lived in the city
that is specified in TL[i*2+1].
TL[i*2+1] = City in which the person lived for TL[i*2] years.
Objective: For this assignment, you will write two programs, one in C and one in MIPS, to
compute the earliest year at which Harry and Sally both lived in the same city. More details are
described below.
Assume that the two people were both born in the year 1990 and the current year is 2019. So the
total number of years (sum of TL[i*2] for i=0,1,…,4) is always 30. Each city is an integer
between 0 and 9, inclusive. Also, assume that the total number of moves in each timeline is
exactly five. For example, the timelines shown in Figure 1 are represented in C as:
HarryTimeline[] = {4, 4, 9, 3, 3, 8, 2, 4, 12, 2};
SallyTimeline[] = {1, 3, 11, 2, 4, 4, 6, 2, 8, 4};
Miami has city code 4 and Harry spent 4 years living there, then moved to Atlanta (city code 3),
where he lived for 9 years. For this example, your program should compute 2008 as the correct
answer.
/* When Harry Met Sally

This program finds the earliest year in which Harry and Sally live in the same
city. This is the only file that should be modified for the C implementation
of Homework 2.

FOR FULL CREDIT (on all assignments in this class), BE SURE TO TRY
MULTIPLE TEST CASES and DOCUMENT YOUR CODE.

*/

#include
#include

#define DEBUG 1 // RESET THIS TO 0 BEFORE SUBMITTING YOUR CODE

/* City IDs used in timelines. */
enum Cities{ London, Boston, Paris, Atlanta, Miami,
Tokyo, Metz, Seoul, Toronto, Austin };

int main(int argc, char *argv[]) {
int HarryTimeline[10];
int SallyTimeline[10];
int NumNums, Year=0;
int Load_Mem(char *, int *, int *);
void Print_Timelines(int *, int *);

if (argc != 2) {
printf("usage: ./HW2-1 valuefile\n");
exit(1);
}
NumNums = Load_Mem(argv[1], HarryTimeline, SallyTimeline);
if (NumNums != 20) {
printf("valuefiles must contain 20 entries\n");
exit(1);
}
if (DEBUG)
Print_Timelines(HarryTimeline, SallyTimeline);

/* Your program goes here */

printf("Earliest year in which both lived in the same city: %d\n", Year);
exit(0);
}

/* This routine loads in up to 20 newline delimited integers from
a named file in the local directory. The values are placed in the
passed integer array. The number of input integers is returned. */

int Load_Mem(char *InputFileName, int IntArray1[], int IntArray2[]) {
int N, Addr, Value, NumVals;
FILE *FP;

FP = fopen(InputFileName, "r");
if (FP == NULL) {
printf("%s could not be opened; check the filename\n", InputFileName);
return 0;
} else {
for (N=0; N < 20; N++) {
NumVals = fscanf(FP, "%d: %d", &Addr, &Value);
if (NumVals == 2)
if (N < 10)
IntArray1[N] = Value;
else
IntArray2[N-10] = Value;
else
break;
}
fclose(FP);
return N;
}
}

/* Colors used to display timelines.
https://en. wikipedia. org/wiki/ANSI_escape_code#Colors */

const char *colors[10] = {"\x1b[30;41m", // red
"\x1b[30;42m", // green
"\x1b[30;43m", // yellow
"\x1b[30;44m", // blue
"\x1b[30;45m", // magenta
"\x1b[30;46m", // cyan (light blue)
"\x1b[30;47m", // white bkgd
"\x1b[30;103m", // bright yellow
"\x1b[30;104m", // bright blue
"\x1b[30;106m"}; // bright cyan

#define RESET "\x1b[0m"

void Print_Years(){
int i;
printf(" ");
for (i=90; i<120; i++){
printf("%3d", i%100);
}
printf("\n");
}

void Print_Timeline(int Timeline[]){
int j, duration, city;
int scale = 3;
char interval[6];
for (j=0; j<10; j=j+2){
duration = Timeline[j];
city = Timeline[j+1];
snprintf(interval, sizeof(interval), "%%%dd", duration*scale);
printf("%s", colors[city]); // start highlighting in city's color
printf(interval, city);
}
printf(RESET); // stop highlighting
printf("\n");
}

void Print_Timelines(int HarryTimeline[], int SallyTimeline[]) {
Print_Years();
printf("H: ");

Print_Timeline(HarryTimeline);

printf("S: ");
Print_Timeline(SallyTimeline);
}

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 21.06.2019 16:30
You have inserted new slides based on a word outline. how do you format these new slides to match the powerpoint presentation formatting? a. select all slides in the presentation and click format on the home tab. b. select the new slides and click reset on the home tab. c. select all slides in the presentation and click reset on the home tab. d. select the new slides and click format on the home tab.
Answers: 2
question
Computers and Technology, 22.06.2019 11:00
The great length of north america causes the climate to be varied. true false
Answers: 2
question
Computers and Technology, 23.06.2019 16:30
20 points archie wants to use a reflector as he photographs a newlywed couple. what would he consider in his choice? a. shadow and sunny b. homemade and professional c. lamps and boards d. incident and reflected e. neutral density and enhancement
Answers: 3
question
Computers and Technology, 23.06.2019 18:50
What is transmission control protocol/internet protocol (tcp/ip)? software that prevents direct communication between a sending and receiving computer and is used to monitor packets for security reasons a standard that specifies the format of data as well as the rules to be followed during transmission a simple network protocol that allows the transfer of files between two computers on the internet a standard internet protocol that provides the technical foundation for the public internet as well as for large numbers of private networks
Answers: 2
You know the right answer?
THE FOLLOWING IS WRITTEN IN C: Summary: This assignment explores the use of basic conditional execu...
Questions
question
Mathematics, 25.02.2020 19:37
question
Spanish, 25.02.2020 19:37
Questions on the website: 13722359