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, 22.06.2019 21:30
Write a function named printfloatrepresentation(float number) that will print the floating point representation of a number using the format given below. (sign bit) exponent in binary (assumed bit).significandfor example if the number passed an argument is 71 yourprogram should print (0) 10000101 (1).00011100000000000000000similarly if the number passed to the function as argument is -71 the program should print (1) 10000101 (1).00011100000000000000000
Answers: 3
question
Computers and Technology, 23.06.2019 16:00
Does read theory have answers keys ?
Answers: 1
question
Computers and Technology, 23.06.2019 16:30
Monica and her team have implemented is successfully in an organization. what factor leads to successful is implementation? good between different departments in an organization leads to successful is implementation.
Answers: 1
question
Computers and Technology, 23.06.2019 17:30
When making changes to optimize part of a processor, it is often the case that speeding up one type of instruction comes at the cost of slowing down something else. for example, if we put in a complicated fast floating-point unit, that takes space, and something might have to be moved farther away from the middle to accommodate it, adding an extra cycle in delay to reach that unit. the basic amdahl's law equation does not take into account this trade-off. a. if the new fast floating-point unit speeds up floating-point operations by, on average, 2ă—, and floating-point operations take 20% of the original program's execution time, what is the overall speedup (ignoring the penalty to any other instructions)? b. now assume that speeding up the floating-point unit slowed down data cache accesses, resulting in a 1.5ă— slowdown (or 2/3 speedup). data cache accesses consume 10% of the execution time. what is the overall speedup now? c. after implementing the new floating-point operations, what percentage of execution time is spent on floating-point operations? what percentage is spent on data cache accesses?
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, 24.05.2021 03:50
question
Mathematics, 24.05.2021 03:50
question
Mathematics, 24.05.2021 03:50
question
Mathematics, 24.05.2021 03:50
question
Mathematics, 24.05.2021 03:50
Questions on the website: 13722362