Answer the question
In order to leave comments, you need to log in
How to display the sum of loop iterations?
I am going through the cs50 course, the task sounds like this:
#include <cs50.h>
#include <stdio.h>
int start_int();
int end_int();
int calculate();
int main(void) {
int i = start_int();
int j = end_int(i);
int h = calculate(j, i);
}
// prompt the user for a starting population size
int start_int(void) {
int y;
do {
y = get_int("set starting population size: ");
} while (y < 9);
return y;
}
// prompt the user for an ending population size
int end_int(start_int) {
int x;
do {
x = get_int("set ending population size: ");
} while (x < start_int);
return x;
}
int calculate(start_int, end_int) {
int k;
int n = 0; //счетчик
do {
k = start_int + (start_int / 3) - (start_int / 4);
} while (k < end_int);
{
n++; //инкремент счетчика
start_int = k; //подмена полученной суммы на "start_int"
return start_int; //возврат замененного значения
}
printf("%i", n); //не выводит сумму итераций
}
~/ $ ./population
set starting population size: 1200
set ending population size: 1300
Answer the question
In order to leave comments, you need to log in
A small recommendation - specify the types of arguments in functions and the arguments themselves in prototypes:
int start_int();
int end_int(int start_int);
int calculate(int start_int, int end_int);
int calculate(int start_int, int end_int) {
int k = start_int; //текущий размер популяции
int n = 0; //счетчик
do {
k = k + (k / 3) - (k / 4);
n++; //инкремент счетчика
} while (k < end_int);
return n; //возврат количества итераций
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question