LastName Unit3 Assignment1

Part 1:
1. A counter controlled iteration structure works on satisfying some condition. The
counter variable is initialized with some value and a condition is checked on every
iteration and only if it satisfies it executes the loop body and when condition is not
satisfies, it exits the loop. The counter variable increments or decrements on each
iteration which controls the loop. Below is an example code:
int i;
for (i = 1; i <= 10; i++)
{
printf("%d ", i);
}
2. A for() statement is a loop statements which iterates over a piece of code on the
basis of a specific condition and keep iterating as long as the condition satisfies.
While switch statement is used to write conditional statement which executes
specific codes on the basis of various condition, it never iterates like a loop. A real
world example would be like when we need to calculate the average grades of
students by adding their individual scores, we will use a loop to iterate over a list of
scores. And when we need to find out the letter grade of students, we will use a
switch statement which will help us in finding the letter grade on the basis of scores.
3. A function is a set of instructions written to do a specific tasks. We would use a
function in C to avoid code duplicity. For example, if there is a set of statements
which is being used at many places in program, we may put it inside a function and
call it where it needs. Three items that are needed to implement a function are
function name, function parameters which are the values passed to the function, and
return type which specifies what type of data is being returned.
4. With functions, we can pass arguments by value or by reference. In pass by value
method, only the copy of variable is passed to the function and any change to it
inside the function has no impact outside it, the value passed to it remains same.
While in pass by reference method, the reference of the variable is passed to the
function, any change to it inside the function reflects the change outside the function.
The caller will have the same modified value that the function has.
5. A recursive function is a function which call itself over again and it terminates
when a specific condition is met. The recursion is related to iteration because there
is a kind of iteration when the same function is called over again and again by itself.
Part 2:
1. The error in the program is that there is not a break statement in one of the switch
options and it runs the default part of the switch. Below is the correct code:
#include <stdio.h>
int main()
{
int count;
printf("Enter the number 1 or 0 for the count: "); // prompt
scanf("%d", &count);
while (count >= 0)
{
switch (count)
{
case 0:
printf("\n%d is the number you entered\n\n", count);
break;
case 1:
printf("\n%d is the number you entered\n\n", count);
break;
default:
printf("\n\nIncorrect Input - You did not enter a 1 or 0 \n\n");
break;
}
printf("Enter the number 1 or 0 for the count: "); // input another
count
scanf("%d", &count);
}
return 0;
}
2. The error is in the condition of if statement, it is an assignment operator but there
should be a comparison operator which compare i with 5. There are 2 negative
impacts of this error, one is the text “Five is my favorite is not getting executed” and
the loop becomes infinite loop and never terminates as on each iteration its value is
changed to 5. Below is the correct code:
#include <stdio.h>
int main()
{
int i;
for (i = 0; i <= 10; i++)
{
if (i == 5)
{
printf("\nFive is my favorite number\n");
}
else
{
printf("\ni is %d\n", i);
}
}
return 0;
}
3. The error in the function is the function sqrt is not known to the compiler, we need
to add the header file math.h to the program which has the method defined. Below is
the correct program:
#include <stdio.h>
#include <math.h>
int main()
{
float count = 9.0; // declare variable
printf("\nThe square root of count is %.1f\n\n", sqrt(count)); // compute the
square root
return 0;
}
4. The error in the program is that function definition is written inside the main
function, it should be outside main function. Below is the correct code:
#include <stdio.h>
int square(int y); // function prototype
int main()
{
int number;
printf("Enter a value: ");
scanf("%d", &number);
printf("\nThe square of the number is %d\n\n", square(number)); //
function call
return 0;
}
int square(int y) // function definition
{
return y*y;
}
5. The error in the program is that there is not a semicolon after function prototype.
Below is the correct code:
#include <stdio.h>
int cube(int y); // function prototype
int main()
{
int number;
printf("Enter a value: ");
scanf("%d", &number);
printf("\nThe numbered cube is %d\n\n\n", cube(number)); //
function call
return 0;
}
int cube(int y) // function definition
{
return y*y*y;
}

Place new order. It's free, fast and safe

-+
550 words

Our customers say

Customer Avatar
Jeff Curtis
USA, Student

"I'm fully satisfied with the essay I've just received. When I read it, I felt like it was exactly what I wanted to say, but couldn’t find the necessary words. Thank you!"

Customer Avatar
Ian McGregor
UK, Student

"I don’t know what I would do without your assistance! With your help, I met my deadline just in time and the work was very professional. I will be back in several days with another assignment!"

Customer Avatar
Shannon Williams
Canada, Student

"It was the perfect experience! I enjoyed working with my writer, he delivered my work on time and followed all the guidelines about the referencing and contents."

  • 5-paragraph Essay
  • Admission Essay
  • Annotated Bibliography
  • Argumentative Essay
  • Article Review
  • Assignment
  • Biography
  • Book/Movie Review
  • Business Plan
  • Case Study
  • Cause and Effect Essay
  • Classification Essay
  • Comparison Essay
  • Coursework
  • Creative Writing
  • Critical Thinking/Review
  • Deductive Essay
  • Definition Essay
  • Essay (Any Type)
  • Exploratory Essay
  • Expository Essay
  • Informal Essay
  • Literature Essay
  • Multiple Choice Question
  • Narrative Essay
  • Personal Essay
  • Persuasive Essay
  • Powerpoint Presentation
  • Reflective Writing
  • Research Essay
  • Response Essay
  • Scholarship Essay
  • Term Paper
We use cookies to provide you with the best possible experience. By using this website you are accepting the use of cookies mentioned in our Privacy Policy.