Starting from:

$30

 Program #1 Conditionals

 Program #1.
Conditionals
Since conditionals tend to be simple enough instead of doing any coding assignments, this assignment will focus more on identifying bugs and mistakes the beginners tend to make.
1 Bug 1
Consider this bit of code:
#include <stdio.h
int main()
{
for(int i = 0; i <10; i++)
{
if(i % 2 == 0)
{
printf(”%d”, i);
}
else {
printf(”Not a even number!”);
return 0;
}
}
}
1
This code just prints out the even numbers from 1 to 10, and if a number is not even, it will
print the number isn’t even. Except it doesn’t. In fact it doesnt work at all. Whats wrong with it?
2 Bug 2
Consider this bit of code:
#include <stdio.h
int main()
{
int age = 0;
int attractiveness = 0;
char sex = ’0’;
scanf(”%d ”, &age);
scanf(”%d ”, &attractiveness);
scanf(”%c”, &sex);
if(age 18)
{
if(sex == ’F’)
{
printf(”Its a match!”);
}
else{
printf(”Look elsewhere”);
2
}
}
else if(attractiveness 7 && age 18)
{
printf(”It’s a match!...for one of you at least”);
}
}
This code is to help my dating profile game. The program will prompt the user to input their age,
attractiveness level, and gender and based on what the user inputted will print whether or not they are
a match for me. If the user is over 18 and is female, the program will print a match (welp. thats sad.)
else if the user has an attractiveness level greater than 7 and they’re also over 18, the program will also
print a match!
However, on the input ”18 10 M”, this code returns the wrong output. Why?
3

More products