Skip to main content

Posts

Call by Value and Call by Reference( pass by value and pass by reference)

There are two ways to pass arguments/parameters to function calls -- call by value and call by reference.. The major difference between call by value and call by reference is that in call by value a copy of actual arguments is passed to respective formal arguments. While, in call by reference the location (address) of actual arguments is passed to formal arguments, hence any change made to formal arguments will also reflect in actual arguments. In C, all function arguments are passed "by value". The calling and called functions do not share any memory -- they have their own copy and the called function cannot directly alter a variable in the calling function; it can only alter its private, temporary copy. There are cases we need call by reference. 1.The called function communicates to the calling function only through return statement and return statement can only send one value back to the calling function. If there are more than one value we want to alter, call by

Functions in C

A function is a self-contained block of program statements that performs a particular task. The concept of C programming is combination of functions and the program execution will begins from the special function called main(). The scanf(), printf() main() etc.. that have been used in programs so far are all functions. They are called library functions and C provides a lot of them.In addition to built in library functions, the programmers can write their own functions and use them.These are called user defined functions. When there are multiple functions in a program , each function is given different name and a list of parameters. When ever the function is to be called, we refer the function name with suitable list of arguments. The program control will then be transferred to the function and the statements in the function will be executed. After the called function is executed, the control returns to the calling function, the place it was called from.A function can call another f

Array and Pointers in C

Pointers and arrays are inseparably related, but they are not synonymous. An array is a non empty set of sequentially indexed element having the same type of data. Each element of the array has a unique identifying index number. Changes made to one element do not affect the other elements. An array occupies contiguous block of memory. Array notation(int a[10]) is a form of pointer notation. The name of an array is the beginning address of the array, called the base address of the array. It is the address of the zeroth element of the array. The array name is the address constant .So *a will get the zeroth element and *(a+1) will get you the first element and so on.The indirection operator * implies that value at address a[i] is equivalent to *(a+i). Execute this program to know the concept #include <stdio.h> int main() { int a[]={10,20,30,40,50}; printf(“%d %u %u %d %u %u %d”,a[0],a,&a[0], *a,&a[1],(a+1),*(a+1)); } Note that a[0] and *a will print the ze

Programs to try using 2D-Array

1.Write programs for matrix arithmetic. a)addition b)subtraction c)multiplication 2.Find transpose of a given matrix.(university question) 3.Find the trace of a matrix.( sum of the diagonal element) 4.Find sum of each rows and columns of a matrix. 5.Find the norm of a matrix.(1-norm .Find sum of absolute value of element column wise.Find the max). 6.Given two square matrices A and B of same order. Check whether AB equal BA. 7.Check whether the given matrix is symmetric.(university question) 8.Check whether a given matrix is an identity matrix. 9. Calculate determinant of a 3 x 3 matrix. 10.Find the saddle points in a matrix. 11.Given matrix A.B,C of size 3x3 find 3A-2B+AC 12. marks(out of 150) of 5 students in 3 subjects are stored in matrix form.Find a)top marks in each subject b)Number of failed students in each subject c)average mark in each subject. d)Number of supply subjects each students have. 13.Print all prime numbers from a matrix.(university question) 14.Write a C program t

Programs to try using 1D-Array

1.Read an array and rotate elements right( read n-number of times to rotate). 2.Find the average of list of numbers. 3.Find the maximum and minimum values from an array of integers. 4.Swap the k th and k+1 th element in an array. Read k. 5.Find the binary equivalent of a number using array. 6.Find similar elements in an array and compute the number of times they occur. 7. Separate odd and even integers in separate arrays from a given array. 8.Find the intersection of two set of numbers. 9.Rearrange n numbers in an array in reverse order.(selection sort) 10.Sort the numbers stored in array in ascending order.(bubble sort) 11.Arrange numbers in an array in such a way that the array will have odd numbers followed by the even numbers. 12.Find the frequency of digits in a set of numbers. 13.Remove duplicate elements from an array. 14.Merge two sorted arrays into another array in sorted order. 15.Find the standard deviation of set of integer data. 16.Read list of numbers an

if else statement, sample programs and programs to try

If else statement It is a decision making statement in C programming language which allows the program to test for oneor more condition(boolean value) and execute set of statements depends on the condition.The statement in the if block is executed if the condition is evaluated to be true.If the condition is false the statements in the else block will be executed. Syntax: if  ( condition)  {     statements in if block  } else {  statements in else block }    Note: in C zero and null values are treated as false any other non zero or non null values are true. the else part is optional. example : #include<stdio.h>  int main()  {  int num; printf("Enter a number \n"); scanf("%d",&num); if(num<10)       { printf("The value is less than 10"); }  else       { printf("The value is greater than 10"); }  return 0;  } if else if statement An if statement can be followed by an optional else if...else statement, which is very useful to test va

Programs to try using loops

Loops 1)Print the even numbers between 0 and 50 (use while) 2)Print the odd numbers between 0 and 50 in the reverse order ( use while) 3)Print the series 5,10,15,….,100 (using for loop) 4)Generate the series 1, 2, 4 ,7, 11 ,16....n ( use while...read n) 5)Generate the Fibonacci series 0 1 1 2 3 5 8…..n ( use do while..read n) 6)Find the factorial of a number ( use for statement do not use built in factorial() function) 7)Print the powers of a number upto 10th power. ( Eg: if n=2 then the program should print 2^0, 2^1, 2^2,2^3,…..,2^10 use for loop) 8)Print the multiplication table of a given number. ( use while) 9)Print the numbers from 1 to 10 and their natural logarithms as a table ( use for) 10)Find the sum of the digits of a number.( use while-university question) 11)Check whether the given 3 digit number is an Armstrong number. (use do while Eg:153,370,371,407) ( uq) 12)Find the factors of a number ( use for) 13)Check whether the given number is prime or not ( use