Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Know terminology/processes/procedures/lists within the assigned readings: Chapte

ID: 3572419 • Letter: K

Question

Know terminology/processes/procedures/lists within the assigned readings: Chapters 1( 1.2 – 1.6), 2, 3 (3.6 will not be included), 4 & 5 Know terminology/processes/procedures/lists within the following lectures: Functions & Overview of computers and c Be able to do the following: Answer and work all Quick-Check Exercises at the end of each chapter. Answers are given in the book. Identify Software development steps as presented in Section 3.1 Identify a function call and input arguments Identify input variables (parameters), output variables and program (local) variables as related to functions Identify function prototypes and function definitions Evaluate some of the functions found in MATH.h (Section 3.2) Evaluate and/or code user defined functions Discuss the advantages of using functions Evaluate mathematical expressions with increment and decrement operators( ++, --) and compound assignment operators (+=, *=, ...) If else statements (single, with compound statements, nested) conditions that utilize &&, ||, and ! with relational operators switch statements while, do...while, and for loops

Explanation / Answer

Function call in C:

Lets start with a program---->

#include<stdio.h>
#include<conio.h> //These are the header files to include

int sum(int,int); //here I have declared a function

void main() //main function to from where compilation starts
{
int a,b,c;
printf(" Enter the two numbers : ");
scanf("%d%d",&a,&b);

c = sum(a,b); //here I want to add the two numbers then simply i call sum() function with taking input parameters as those numbers separated wih comma

printf(" Addition of two number is : %d",c);
getch();
}

int sum (int num1,int num2) //definition of function to call somewhere when needed
{
int num3;
num3 = num1 + num2 ;

return(num3);
}

To recognise the arguments and input parameters:

In the above program,

I have used a function sum() and called it when i needed the sum of two numbers.
So in the line I have called the function,I have to send two numbers to get its sum and those are the parameters
of the function sum().

c = sum(a,b);
or
c = sum(10,20);//Here two numbers are parameters

Now,see in the function definition:


int sum (int num1,int num2) //num1 and num2 are the Arguments
{
int num3;
num3 = num1 + num2 ;

return(num3);
}

In this, the function accept those numbers parameters in the form of Arguments.So,here num1 and num2 are two arguments
of the function which can be used to use two numbers send while calling the function.

Math.h functions:


<math.h> in C is a header file need to be included in the program whenever any mathematical functions used.
All the functions available in this library takes double as an argument and return double as the result.


example: 1.Let suppose to find the value of x to the power y,

then we call the function,
double z = pow(x,y)//where both x and y are double and the value returns is also double

2.Now to find logarithm value of any value x,

then use, double y = log(x);


Diffecrence between post or pre increment or decrement operators:

post operator --> x++ or x-- i.e.value of x is incremented or decremented after assigning it to the variable x. Means first use the value then increment or decrement it.

pre operator --> ++x or --x i.e.value of x is incremented or decremented before assigning it to the variable x. Means first increment or decrement the value then use it.


compound assignment operators:

these operators are simply used to assigning values to a variable, that's why they are called assignment operators.
we have so many :

1. =   ------> used as a = b   ----> to implement same a = b
2. +=   ------> used as a += b ----> to implement   a = a+b
3. -=   ------> used as a -= b ----> to implement   a = a-b
4. *=   ------> used as a *= b   ----> to implement a = a*b
5. /=   ------> used as a /= b   ----> to implement a = a/b
6. %=   ------> used as a %= b   ----> to implement a = a%b


if else conditions in C:


if else conditions works i the same way it sounds actually.


Like suppose we have a situation, if we have money i will buy a pen and if I don't have any money, I will not buy any thing.

we can implement it like:

if(I have money) I will buy pen
else I will not buy

Now, We can have so many situations like to check multiple conditions with different priority:

example:

if(I have money){
if(I have more then 50$) i will buy a book
else i will buy a pen only
}
else i will not buy anything

So, whenever we use if-else inside another if-else then its called as nested if-else.

if-else using operators:

there are different operators used in checking the conditions like:

1.&& ----> this operator is used to check two mandatory conditions like we must have both condition applied otherwise it will goes to else.


Example: if(I have money && I have more then 50$){}

2.|| ----> this operator is used to check two options like we must have any one of the conditions applied otherwise it will goes to else.


Example: if(I have money || I have less then 50$){}


3.== ----> this operator is used to check two conditions are equal or not otherwise it will goes to else.


Example: if(I have money == 50$){}


4.!= ----> this operator is used to check two conditions must not be equal otherwise it will goes to else.


Example: if(I have money != 50){}

LOOPS:

1.for loop:


for loop consecutively checks the condions and implement the code:
Example-->


int main () {

int a;
  
/* for loop starts*/
for( a = 10; a < 20; a = a + 1 ){ //checks the condition here whether a is smaller than 20 or not
printf("value of a: %d ", a);
}

return 0;
}

2.while() loop:

while loop is similar to for loop in implementation.Only different thing is that, it only checks the condition in its first statement.

Example:

int a=0;

while(a < 20){

printf("value of a: %d ", a);
a++;

}

3.do-while:


do-while is also implemented in same way but the difference is it always implement it in first iteration in any condition and then checks the condition from second iteration.


int a=0;

do{

printf("value of a: %d ", a);
a++;

}while(a < 20);


Switch case:


Switch cases are mainly used whenever we need to check any value against a list of values.Lets have an example :

if variable "a" can have so many values 1 to 5, and need to implement some specific code on every value.
then we use it like:


switch(a){
case 1: statement;
case 2: statement;
case 3: statement;
case 4: statement;
case 5: statement;
}

So, if "a" equals 1 then it implements code of case 1, and so on.
Also if we need to implement only the condition matched first then we use break; like:


switch(a){
case 1:statement break;
case 2:statement break;
case 3:statement break;
case 4:statement break;
case 5:statement break;
}


And if we need to implement code in any way, we use "default",


switch(a){
case 1:statement break;
case 2:statement break;
case 3:statement break;
case 4:statement break;
case 5:statement break;
deafult: //optional
}