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

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 17:40, lazerlemon500
Write a modular program (no classes yet, just from what you learned last year), that allows two players to play a game of tic-tac-toe. use a two-dimensional char array with 3 rows and 3 columns as the game board. each element of the array should be initialized with an asterisk (*). the program should display the initial board configuration and then start a loop that does the following: allow player 1 to select a location on the board for an x by entering a row and column number. then redisplay the board with an x replacing the * in the chosen location. if there is no winner yet and the board is not yet full, allow player 2 to select a location on the board for an o by entering a row and column number. then redisplay the board with an o replacing the * in the chosen location. the loop should continue until a player has won or a tie has occurred, then display a message indicating who won, or reporting that a tie occurred. player 1 wins when there are three xs in a row, a column, or a diagonal on the game board. player 2 wins when there are three ox in a row, a column, or a diagonal on the game board. a tie occurs when all of the locations on the board are full, but there is no winner. input validation: only allow legal moves to be entered. the row must be 1, 2, or 3. the column must be 1, 2 3. the (row, column) position entered must currently be empty (i. e., still have an asterisk in it).
Answers: 1
image
Computers and Technology, 23.06.2019 00:20, kathlynskare06
Ihave been given the number of guns per 100, and the total firearm-related deaths per 100,000. i have to find the actual number of guns per country and actual number of gun-related deaths. if somebody could show me how to do 1 question, i can finish the rest, i am just confused. tia
Answers: 3
image
Computers and Technology, 23.06.2019 06:20, Ab20600
Which text function capitalizes the first letter in a string of text? question 10 options: upper capital first proper
Answers: 1
image
Computers and Technology, 24.06.2019 03:30, laylay120
Other - a written response, no less than arial 12-point font, to the following: of the following, which would you consider is most important to customer service goals? choose one and explain why. (1) accuracy (2) punctuality and attendance (3) courtesy (4) productivity (5) organization
Answers: 1
You know the right answer?
THE FOLLOWING IS WRITTEN IN C: Summary: This assignment explores the use of basic conditional execu...

Questions in other subjects:

Konu
English, 02.02.2021 23:50
Konu
History, 02.02.2021 23:50