Write program algorithms using arrays, pointers & structures.
a) Implementation of basic arithmetic operations using switch – case statements and functions.
Functions: Function is a portion of code within a larger program, which performs a specific task and can be relatively independent of the remaining code. The syntax of many programming languages includes support for creating self contained subroutines, and for calling and returning from them.
Program Algorithm:
i. Get two numbers and the arithmetic operation to be performed on them as input.
ii. Use a switch case statement
iii. According to the selected option the desired function is called from inside a switch case function.
iv. The result is printed.
v. The steps are repeated until the user wishes to quit.
b) Creating an employee database using structure.
Structures: A structure provides a means of grouping variables under a single name for easier handling and identification. A structure is declared by using the keyword struct followed by an optional structure tag followed by the body of the structure. The variables or members of the structure are declared within the body.
Program Algorithm:
i. Create a structure using the struct variable for each employee containing the necessary attributes of that employee.
ii. Create an array of such structures for n number of employees.
iii. Get the input of each attribute for each employee from the user.
iv. Calculate the gross salary by the following formulas.
hra (10 %), da(50%), ta(5%), pf(11%), tax(6%) from basic.
Gross = basic + hra + da + ta
Deduction = pf + tax
Netsalary = gross – deduction
v. Display the results.
c) Swapping of two numbers using call by reference.
Pointers: Pointers are variables that hold addresses in C. A pointer is a special type of variable that contains a memory address rather than a data value. Just as data is modified when a normal variable is used, the value of the address stored in a pointer is modified as a pointer variable is manipulated. A pointer to the variable is passed to the function. The pointer can then be manipulated to change the value of the variable in the calling routine. It is interesting to note that the pointer itself is passed by value. The function cannot change the pointer itself since it gets a local copy of the pointer. However, the function can change the contents of memory, the variable, to which the pointer refers. The advantages of passing by pointer are that any changes to variables will be passed back to the calling routine and that multiple variables can be changed.
Program Algorithm:
i. Get the numbers two be swapped
ii. Pass the elements using their address to the swap function
iii. Receive the parameters for the swap function using pointers
iv. Perform swapping inside function
v. Display the result in the main function
Post A Comment:
0 comments: