s7.c
that gets a positive integer from the user and puts it in variable
top
.
It then finds and prints the sum of the first top positive numbers:
(1+2+3+...+top). For example, if the user's input is 5, then program
should find 1+2+3+4+5 and print the result: 15. It should print only
the final result.
s8.c
.
It should get a positive integer from the user and store it in the
variable, cutoff
. Then it should find and print the sum of
all positive odd numbers less than the cutoff. For example if the
user's number is 11, then
the program should add (1+3+5+7+9) and print the result 25.
Once again the program should print only the final result.
s9.c
.
It should get a positive integer from the user and store it in the
variable, numOdds. Then it should find and print the sum of
that many odd numbers. For example if the user's number is 4, then
the program should add (1+3+5+7) and print the result 16.
Once again the program should print only the final result.
s10.c
.
a positive integer from the user and store it in variable
increment. It should find and print the sum of 100 numbers.
The first number is 1. Each new number is larger than the last
by the user's number.
Example: Suppose the user's number is 3. The the sum will start:
1+4+7+10+13+ ... Once again the program should print only the final result.
s11.c
.
It should get
a positive integer from the user and store it in variable start
.
It should find and print the sum of consecutive positive numbers from
start
up to 100:
start + (start+1) + (start+2) + ... + 99 + 100Once again the program should print only the final result.
char str[100]; fgets(str, 100, stdin);Now we want a loop like the loop in the last example in section 3. We don't need to print the characters. We are more interested in the index,-- the number in the brackets.
char str[100];Then it uses
fgets(str, 100, stdin)
to get the
first string at the beginning of the array. The first argument is str since str
is the symbol for the start of the array. Suppose the user enters
the string DOG. So stored in the first five places we have
'D' 'O' 'G' '\n' 0 ^ str+4 ^ str+3 ^ str+2 ^ str+1 ^ strNow we want to use fgets again to get the second string.
fgets(??, ??, stdin);We want the second string to start right after the end of the first. Technically the newline character code, '\n', is part of the string, but to the user the last meaningful thing he or she typed was the G. Thus we want the second string to start at the address now containing the newline character code, '\n'. So the second fgets now looks like
fgets(str+3, 100-3, stdin);Note the second argument is 100-3, since we've used up three positions in the array so there are only 97 positions left. So the second fgets will put the new string right after the G. This works fine if the first string has 3 characters. But if the first string has say, 5 characters before the newline then the second fgets would be:
fgets(str+5, 100-5, stdin);So to handle any string that might be entered, you are going to have to use a variable:
int nlPos; //get the position of the newline into variable, nlPos fgets(str+nlPos, 100-nlPos, 100-stdin);
goodbye
, then the program should print:
goodbye oodbye odbye dbye bye ye e
bd1.c
. This program
expects input to be a file of decimal integers.
It is to find the sum
of the input integers. It is to print only one value: the final total.
bd2.c
. This program
expects input to be a file of decimal integers.
It is to count the
number of input integers greater than or equal to 100. It is to print
only one value: the final count.
bd3.c
. This program
expects input to be a file of decimal integers.
It is find the sum of all input numbers that are less than 100.
It is to print only one value: the final sum.
bd4.c
. This program
expects input to be a file of decimal integers.
It is to find the largest number in the file. It should print only this
number. Be careful: Suppose all the numbers are negative.