CS50-Week1

文章发布时间:

最后更新时间:

文章总字数:
548

预计阅读时间:
3 分钟

Lab1 C:Population Growth

How to do in this Lab

  1. Log into cs50 online devEnviroment
  2. Log into submitSystem
  3. When you finish any Labs,execute check50 in teminal follow the sign$
  4. After pass the check,execute style50 to decrate your code,and then submit by the command submit50.You can see practiced examples below right now.

Lab 1

Before the question

  1. Input mkdir populationto make a dir(i.e. directory) for your code by terminal.

  2. Then execute cd population to enter the dir just created,and you should see the prompt turn to population/ $ .

  3. Type wget https://cdn.cs50.net/2022/fall/labs/1/population.c from CS50 .

  4. You can have a look what inside by typing ls ,then use code population.c to start your coding.


Background

Say we have a population of n llamas. Each year, n / 3 new llamas are born, and n / 4 llamas pass away.

For example, if we were to start with n = 1200 llamas, then in the first year, 1200 / 3 = 400 new llamas would be born and 1200 / 4 = 300 llamas would pass away. At the end of that year, we would have 1200 + 400 - 300 = 1300 llamas.

To try another example, if we were to start with n = 1000 llamas, at the end of the year, we would have 1000 / 3 = 333.33 new llamas. We can’t have a decimal portion of a llama, though, so we’ll truncate the decimal to get 333 new llamas born. 1000 / 4 = 250 llamas will pass away, so we’ll end up with a total of 1000 + 333 - 250 = 1083 llamas at the end of the year.


My Thoughts

Easy to understand the question,every year the number of llamas will be plused 1/3 and at the same time mintued 1/4.

We want to know after how many years that numbern do equal the given numberm,and output the years.

After finishing the code ,time to check,debug,style and submit.

test before check in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//How to Test Your Code
//Your program should behave per these examples below.

$ ./population
Start size: 1200
End size: 1300
Years: 1
$ ./population
Start size: -5
Start size: 3
Start size: 9
End size: 5
End size: 18
Years: 8
$ ./population
Start size: 20
End size: 1
End size: 10
End size: 100
Years: 20
$ ./population
Start size: 100
End size: 1000000
Years: 115

As soon your code works,you can check and so forth.

Following the commend below steo by step,and remember you are in the right dir.

1
2
3
check50 cs50/labs/2023/x/population
style50 population.c
submit50 cs50/labs/2023/x/population

Here are my final code styled by the CS50.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <cs50.h>
#include <stdio.h>

int main(void)
{
// TODO: Prompt for start size
int n;
int y = 0;
do
{
n = get_int("Start size:");
}
while (n < 9);
// TODO: Prompt for end size
int m;
do
{
m = get_int("End size:");
}
while (m < n);
// TODO: Calculate number of years until we reach threshold
while (n < m)
{
n = n + n / 3 - n / 4;
y++;
}
// TODO: Print number of years
printf("Years: %i", y);
}

Courses and other relative URLs