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);
}