0% found this document useful (0 votes)
21 views22 pages

c&c++ 2nd 3rd

The document discusses loops and arrays in C and C++, detailing types of loops (while, do-while, for) and their syntax with examples. It also covers jumping statements (break, continue, goto) and provides an overview of arrays, including their declaration and initialization. Additionally, it explains multi-dimensional arrays and string handling functions, including reading and writing strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views22 pages

c&c++ 2nd 3rd

The document discusses loops and arrays in C and C++, detailing types of loops (while, do-while, for) and their syntax with examples. It also covers jumping statements (break, continue, goto) and provides an overview of arrays, including their declaration and initialization. Additionally, it explains multi-dimensional arrays and string handling functions, including reading and writing strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Programming with C & C++ B.

Com

Unit-2
Loops and Arrays

1. Discuss about looping statements or Iterative


statements Or Repetitive statements with an example?
Or
Explain about the use of while loop, do-while loop and for
loops
 Looping statements are also called iterative statements or repetitive statements.
 A loop is used to execute the block of statements repeatedly until the condition is false.
 C language supports 3 types of looping statements they are:
While loop
Do-while
loop For
loop
1. While loop/Top checking loop/Entry Controlled
Loop
 While loop is looping statements.
 While loop is used to execute the statements repeatedly until the condition is false.
 While is loop is also called Top-checking loop and Entry Controlled loop.
 The condition is checked first. If the condition is true then executes the statements
until the condition is false.
Syntax:
while (condition)
{
Statements block;
Increment/decrement
} s;
Example:

#include<stdio.
h>
#include<conio
.h> void main()
{
int
x=1;
clrscr();
Programming with C & C++ B.Com
while(x<6)
{
printf(“\n B.Com 2nd
Year”); x++;
}
getch();
}
Output:
B.Com 2nd
Year B.Com
2 nd
Year
B.Com 2nd
Year B.Com
2 nd
Year
B.Com 2nd
Year
2. Do-while loop/Exit controlled loop
 do while is same as while loop.
 It is also looping statement.
 Do-while loop is used to execute the statements repeatedly until the condition is false.
 First It is used to execute the statements then check the condition its true or not.
 Hence it is called Exit controlled Loop.
Syntax:
do
{
Statement block;
Increment/decremen
t;
}
While (condition);
Example:
#include<stdio.
h>
#include<conio
.h> void main()
{
int i=1;

2
Programming with C & C++ B.Com
clrscr();
d
o
{
printf(“\n This is a program of do while
loop.”); i++;

while(i<=5);
getch()
;
}
Output:
This is a program of do while
loop. This is a program of do
while loop. This is a program of
do while loop. This is a
program of do while loop. This
is a program of do while loop.
3. for loop/Top Checking loop/Entry Controlled Loop
 The for loop is the looping statements.
 The for loop is also called Top-Checking loop or Entry controlled loop.
 It is used to execute the set of statements when the condition is true.
 The for-loop statement contains 3 expressions or conditions separated by semicolons.
 There is initialization, condition, increment or decrement at only one statement.
Syntax of for loop statement
for(initialization; condition; increment/decrement/update)
{
Statement block;
}
Output:
#include<stdio.
h>
#include<conio
.h> void main()
{

3
int i; Programming with C & C++ B.Com
clrscr();
for(i=1;i<=15;i=i++)
{
printf(“%5d”,i);
}
getch();
}
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

2. Explain about jumping statements with illustration? Write


notes on break, Continue and goto statements?
 Jumping statements are also called unconditional statements.
 Jump statements are used to interrupt the normal flow of program.
 Unconditional statements are break, continue goto statement.
 An unconditional statement does not have conditions.
Break statement:
 Break statement is jumping statement or unconditional statement.
 It is the keyword.
 No header file is not needed to use break keyword.
 Break statement is also used to terminate looping statements like while, do-while
and for loop using if statement.
 Break statement is used to terminate the switch case statement.
Syntax:
while(condition)
{
Statements;
Increment/decremen
t; if(condition)
{
break;
}
}

Example:
#include
4
<stdio.h>
#include<conio.
Programming with C & C++ B.Com
h> void main ()
{
int a =
10;
clrscr();
while( a < 20 )
{
printf("value of a: %d\n",
a); a++;
if( a==15)
{
break;
}
}
}
Continue Statement:
 Continue statement is unconditional statement or jumping statement.
 It is the keyword.
 No header file is not required to use continue statement.
 The continue statement is exactly opposite of the break statement.
 The continue statement is used for continuing the next iteration of the loop
statements like for loop, while loop and do-while loop using if statement.
Syntax: while (condition)
{
Statements;
Increment/decremen
t; if(condition)
{
continue;
}
}

Example:
#include
<stdio.h>
#include<conio.

5
h> void main ()
{
Programming with C & C++ B.Com
int a = 10; clrscr();
while( a < 20 )
{
printf("value of a: %d\n",
a); a++;
if( a==15)
{
continue;
}
}
}
goto statement:
 The goto statement is unconditional statement or jumping statement.
The goto is the Keyword.
 No header file is not required to use goto statement.
 The goto statement is used to jump from one line to another line in the program.
Syntax:
goto label;
Example:
#include<stdio.
h>
#include<conio
.h> void main()
{
clrscr();
printf(“’hellow world\n”);
goto label1;
printf(“’how are
you?”); printf(“are
you ok”); label1:

printf(“hope yoou are fine”);


}
3. Write about array? Explain its Declarations and
initialization? Definition:
 Array means collection.An array is a collection of elements of the same data
type.
 It can store elements in unique (different) and successive (Side by Side)
memory locations.
6
 An array is a linear data structure. It means all it items form a sequence.
Programming with C & C++ B.Com
Some Array
Terms:
Siz
e
 Number of elements or capacity to store elements in an array is called its size.
 It is always mentioned in square bracket ([]).

Type
:
 Types refer to data type.
 It decides which type of element is stored in the array.

Bas
e:
 The address of the first element (0th) is a base address.
Index/
subscript:
 Index or subscript is used to access the items in array.
 An index starts with 0 and ends with n-1.

Word
:
 It indicates the space required for an element.

Declarations of Array/Creating An array:

7
 Declaration of a one-dimensional array can be done with data type first.
Programming with C & C++ B.Com
 Then followed by the variable name.
 Lastly the array size is enclosed in square brackets.
Syntax:
Datatype Arrayname [size];

Ex: int x[5];

Initialization of One-dimensional Arrays:


The initialization of one-dimensional array means it creates the memory for the
array based on size and it assigns some values during the creating of the memory.
Syntax: data-type variable-name[size] = {value0, value1, value2,... };

4. A) Write about two dimensional arrays?


B) Write about three dimensional arrays?
Multidimensional Arrays
 Data in multidimensional arrays are stored in tabular form (in row major
order) or (column major order).
 The two dimensional (2D) array in C programming is also known as matrix.
 A matrix can be represented as a table of rows and columns. So 2d array is
useful to implement matrix or table.
Two-dimensional Arrays (2-d arrays)
 An array with two subscripts is termed as two-dimensional array.
 It means it have two subscripts or index. First index for row and second
index for columns.
 One-dimensional array can store a row of elements. So a two-dimensional
array permits us to store multiple rows of elements that are a table of
values or a matrix.
Declaration of two-dimensional arrays:
Syntax of two-dimensional array:
data-type variable-name[rowsize][colsize];
8
Example
int b[3][3];
Programming with C & C++ B.Com

[0] [0] [0][1] [0][2]


[1][0] [1][1] [1][2]
[2][0] [2][1] [2][2]
Initialization of two-dimensional arrays:
First form:
First form of initializing a 2-d array is as follows:
data-type variable-name[rowsize][colsize]={initializer-list};
Example:
int a[2][3] = { 2, 3, 4, 5, 6, 7};
2 3 4
5 6 7
Second form:
data-type variable-name[rowsize][colsize] = { {initializer-list1},
{initializer-list2}, …….};

Example:
int a[2][3] = { { 4, 5, 9 }, {6, 7, 8 }};
4 5 9
6 7 8
Three-dimensional Arrays: OR Multi Dimensional arrays
 An array with three subscripts is termed as three-dimensional array.
 A three-dimensional array is a collection of one or more two-dimensional
arrays, all of which share a common name.
 Two-dimensional array is used to store a table of values. So, a three-
dimensional array is used to form a group of tables and perform collective
manipulations.
Declaration of three-dimensional arrays:
Syntax of declaration of a 3-d array is as follows:
data-type variable-name[size1][size2][size3];
size1 indicates the no. of tables being grouped together.
size2 indicates the no. of rows of each table.
size3 indicates the no. of columns of each table.
Example:
int a[2][4][5];

9
Programming with C & C++ B.Com

Unit -3 .

Chapter 1: Strings
1. What is meant by String? How can you Read and write A string in C? Or

1. Write about string accessing modes with examples? Or

1. Discuss in a detail about String Storing functions? Or

1. Write about declaration and Initialization of Strings?

 A string is defined as a collection of characters.


 Strings are used to handle text, word and sentence.
 Every string is terminated with ‘\0’ (NULL) character.
 The length of the string is defined as number of characters in string.
 The letters are accessed using index or subscript.
 The first letter is indexed as 0.
Declaration of string/Creating of String:
 The declaration of string means creating memory for string variables.

Syntax:

Data type variable name [size];

Example:
char name[12];

Initialization of string:
 The initialization of strings means creating memory and assigning values for

10
string variables.
Programming with C & C++ B.Com
Syntax:
Data type variable name[size]={characters];

Memory map of a string:

Example: Write a c Program to display a

string. #include<stdio.h>

#include<conio.h

> void main()

char str[] = "computer";

clrscr(); printf("%s",str);

Output: Computer

2. Explain the string reading and writing functions? Or


11
2. Write about How to read a string and writing functions?
Programming with C & C++ B.Com

Reading functions and Writing functions


 Reading functions are input functions and writing functions are output
functions.
 Reading functions are scanf(), gets(), getch().
 Writing functions are printf(), puts(),
putch().
The string can read from the user by using 3 ways:
1.scanf() function

2. gets() function

3. getchar() getch() functions

1. scanf() function:
 It is the input statement.
 This function is used to receive the characters or strings and store them in
variables.
Syntax:
scanf(“control string”, variable name);

2. printf() function:
 The printf() function is output statement.
 This function is used to display the values of the variables.
 The printf() displays the entered character or string.
Syntax:
printf(“control string”, variable name);

Example:
#include<stdio.h

> void main()

char name[5];

printf(“entered string is:%s”);

12
scanf(“%s”,name);
Programming with C & C++ B.Com

gets() function:
 The gets function stands for get string.
 This function is used to read keystrokes and assigns to the variable.

Syntax:
gets(variable name);

puts function:
 The puts function stands for put string.
 This function is used to display/print the string or character array.

Syntax:
puts(variable name);

Example:
#include<stdio.h

>

#include<conio.h

>

void main()

char c[30];

clrscr();

gets(c);

puts(c);

13
Programming with C & C++ B.Com

getchar() or getch() or getche():


 These functions read any alphanumeric characters from the standard input
device.
 The getch() accepts character but does not display the character.
 The getche() accept character and display the character.
Syntax:

getche();

putchar() or putch() function:


 This function prints one character on the screen at a time.
Syntax:
Variable name=putchar(variable name);

Example:

#include<stdio.h

> void main()

char ch;

ch=getchar();

putchar(ch);

3. Write about String manipulation functions or String Handling functions?


Or

Explain about Standard C string library functions?


String manipulation functions / string handling functions:

 String manipulation is defined as maintain the string.


 Every c compiler supports a large number of strings handling library
functions.
 You must use header file #include<string.h> to utilize
the following functions.

Functions Description
strlen() Length Function
14
strcpy()
strncpy()
Programming
Copy Function
with C & C++ B.Com
Copy of „n‟ character Function
strcat() Concatenate Function
strcmp() Compare Function
strchr() Search character Function
strlwr() Lower Case Function
strupr() Upper Case Function
strrev() Reverse Function

1. strlen(): - Length Function : This returns the number of characters in the


sting.

Syntax: - strlen(“string”); Example: - strlen(“computer”);  returns : 6

2. strcpy(): - Copy Function: This function is used to copy one string to


another string.
Syntax: - strcpy(“String1”, “String2);
Example: - char str1[10] = {“programming”}
char
str2[10] = {“Language”} strcpy(str1,
str2)  returns : Language
3. strncpy(): - Copy of „n‟ character Function: This copies „n‟ characters of
the second string to the first string.
Syntax: - strncpy(“string1”, “string2”, n );
Example: - strncpy(“Delhi”, “Bangalore”, 6); returns: Bangal.
4. strcat(): - Concatenate Function: This function is used to concatenate
two strings. That is, it appends one string at the end of the specified string.

Syntax: - strcat(“string1”, “string2”);


Example: - strcat(“goood”, “morning”); return: goodmorning
5. strcmp(): - Compare Function: This function compares two string
character by character (ASCII Comparison) and returns one of the three
values { -1, 0 , 1}.
Syntax: - strcmp(“string1”, “string2”);
Example: - strcmp(“ABC”, “abc”); return : -32 (65-97)
= -32.
NOTE: -Here ASCII value of „A‟ (i.e 65) is less than the „a‟ (i.e 97). Thus the
above statements return -32.

6.strchr(): - Search character Function: This function searches for a


specified

character in a given string. It return Null if the desired character is not found.

Syntax: - strchr(“string1”, desired_char);


15
Example: - strchr(“Degree”, „D‟); returns: D
Programming with C & C++ B.Com
Example: - strchr(“BCOM”, „H‟); returns: Null.

7. strlwr(): - Lower Case Function: This function is used to convert any


uppercase letters into lowercase.

Syntax: - strlwr(“string”);
Example: - strlwr(“TELUGU”); return: telugu

8. strupr(): - Upper Case Function: This function is used to convert any


lowercase letters into uppercase.

Syntax: - strupr(“string”);
Example: - strlwr(“english”); return: ENGLISH.

9. strrev(): - Reveres Function: This function can convert into right to left
characters.

Syntax: - strrev(“string”);
Example: - strrev(“MOBILE”); return: ELIBOM
4.What is meant by Function? Explain about Function Prototype /Function
Declaration?
(or) Explain about different types of functions in C? (or)
Define function? Explain the implementation of the functions in C?
A function is a self-contained block or a sub-program of one or more statements that perform a
special task when called.
A function is identified by function name, parentheses, opening braces and closing parentheses.

main()
{
func1( )
.......
{
.......
}
fun1()
........
return 0;
}

In the above figure illustrate there are two functions are there main function and func1 is
another function.

16
Programming with C & C++ B.Com

Figure 1 A program divided in multiple functions

Types of functions:
The C language supports two types of functions
(1) Library functions or Built-In functions or System Defined Functions
 Library functions are also called Built-in functions or system defined functions.
 These functions can be developed by the special programmers
 The library functions are pre-defined set of functions.
 Their task is limited.
 One can only use the functions but cannot change or modify them.
Ex:
main(), clrscr(), getch() put()and getche()
(2) User-defined functions
 The User-defined functions can be developed by the user.
 The user-defined functions are not pre-defined functions.
 A user can modify the functions according to the requirement.
Function terminology
Called function and calling function:
 main() function calls the function name func1(). Therefore main() is called as calling
function and func1 is known as called function.
Argument or parameter types:
 The function takes input from main function is called argument or parameter.
 Arguments can be divided into 2 types like actual parameter and formal parameters.
 Actual parameters refers to the arguments of calling functions are called actual
arguments.
 Formal parameter refers to the arguments of called functions are called formal
parameters.
return statement:
 return statement is used to return the values to the calling function.
Function declaration/ Function Prototype
 Function declaration means creating of memory for that function.
 It is terminated by semicolon;

Syntax:
 return type data type function name(data type variable name1…….);
Function definition:
 Function definition means body of the function. It contains executable code.
Syntax:

17
Programming with C & C++ B.Com

Ex:
Write a program to show how user-defined function is called.
#include<stdio.h>
int add(int a, int b) /*function definition*/
{
return(a+b);
}
void main()
{
int x=1,y=2,z;
z=add(x,y); /* FUNCTION call/
printf(“z=%d”,z);
}
Output:
Z=3

2. Describe the approach of passing argument/parameters to functions?


(OR)
Explain the following briefly:
Call by value or pass by value
Call by reference or pass by reference
There are two ways in which we can pass arguments to the function.
 Call by value
 Call by reference
Call by Value
 Arguments are passed by value is called call by value or pass by value.
 New variable are created to store the values of the arguments.
 In call by value return statement is used.

Syntax of the Call by value


return type function name (argument 1, argument2)
{
return statement;
}

Call by reference:
 Arguments are passed by address is called call by reference or pass by reference.
 Called function can work on address of the arguments than values.
 In call by reference the return statement is not required.

18
Syntax:
Programming with C & C++ B.Com
return type function name(*argument1, *argument2)
{
Statements;
}
5. Define Recursive function? Explain with illustration
A function is calls itself to solve the problem is called recursive function. The best
example of recursive function is finding the factorial number.

Example:

n ! = n x ( n - 1)

Let us say we need to find the value of 5!.....

n!= n x ( n - 1)
5!= 5 x (5-1) 2!= 2x1

5!= 5x4 1!= 1 x (1-


1)
4!= 4 x (4-1) 1!= 1 x0

4!= 4x3

3!= 3 x (3-1)

3!= 3x2

2!= 2 x (2-1)

Recursions are of two types:

(i) Direct recursion (ii) Indirect recursion.

Direct

A function calls itself; this type of recursion is direct recursion. Only one function
is involved in direct recursion.

19
Programming with C & C++ B.Com
Ex:

int num()

___

num(

);

Indirect

Two function calls each other is known as indirect recursion.

Ex:

int num()

___

sum()

;
}

sum(
)

___

num(
}
);

20
Write a C program to compute factorial number using recursive function

#include <stdio.h>

int Fact(int); // FUNCTION DECLARATION

void main ()

int num;

printf("\n Enter the number: ");

scanf (“%d", &num) ;

printf("\n %d , num,);

Fact(num)); // FUNCTION CALL

int Fact( int n) // FUNCTION DEFINITION

if(n=

=l)

return

1;

return (n * Fact(n-l));

You might also like