联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2020-11-19 11:33

UESTC 1005 – Introductory Programming Page 1 of 2

Lab Session - Pointers

Exercise 1: Pointers

Background

Pointers: stuff from lecture notes.

To keep track of time, computers need something to measure it against. In C on the lab

computers, we get the number of seconds since 00:00:00 UTC on 1 January 1970.

As you might imagine, this produces a fairly large number -- it exceeds the bounds of a

normal int. We therefore use a long int, which has twice the number of bits of a

normal int. Our time.h library also defines a special data type to store the time value:

time_t. On our computers, this is the same as a long int.

Technical details

Swapping data with pointers:

#include <stdio.h>

void swap(int *px, int *py) {

int temp = *px;

*px = *py;

*py = temp;

}

int main() {

int x = 1;

int y = 2;

printf("initial: %i %i\n", x, y);

swap( &x, &y);

printf("swapped: %i %i\n", x, y);

getchar();

return 0;

}

Watch out for the * and &

Getting the time:

#include <stdio.h>

#include <time.h> // extra include

int main() {

time_t now;

now = time(NULL);

long int a = now;

printf("%li\n", now); }

UESTC 1005 – Introductory Programming Page 2 of 2

getchar();

return 0;

}

Your task...

Calling time(NULL) gives you a large integer. Use this value to calculate today's year,

date, hours, and minutes.

? Simplifying assumption: we pretend that each year has exactly 365 days. The 15th of

March is therefore day 83 in year 2011. (use that date to figure out today's "day

number")

(hint: how many seconds are there in a day?)

? You cannot use struct for this assignment.

? You cannot use any time-based functions from the standard library such

asasctime or strftime.

? You must use one function to calculate the year and day (pass in the total seconds,

and a reference to the year and day).

? You must use a separate function to calculate the hours and minutes. (again, pass the

total seconds, and a reference to the hours and minutes)

? You must use a separate function to print the time; this function can only take as

arguments the year, day, hour, and minutes.

(optional: handle leap-years correctly, and print the month as well)

Note: Show your work to a demonstrator/GTA.


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp