0% found this document useful (0 votes)
29 views

Chapter 4

Uploaded by

Priyansh Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Chapter 4

Uploaded by

Priyansh Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

PPS (3110003)

CHAPTER 4
Array & String

Q.1 Write a C program to read 10 numbers from user and store them in an array.
Display Sum, Minimum and Average of the numbers.(PPS_WIN2019)

#include <stdio.h>

int main()
{
int i, num;
float total = 0.0, average;
printf ("Enter the value of N \n");
scanf("%d", &num);
int array[num];

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

for (i = 0; i < num; i++)


{
scanf("%d", &array[i]);
}

printf("Input array elements \n");

for (i = 0; i < num; i++)


{
printf("%+3d\n", array[i]);
}

/* Summation starts */

for (i = 0; i < num; i++)


{
total+=array[i];/* this means total=total+array[i]; */
}

average = total / num;

printf("\n Sum of all numbers = %.2f\n", total);

printf("\n Average of all input numbers = %.2f\n", average);


}

CKPCET, SURAT 1
PPS (3110003)

Q.2 Why null value is used in string? Justify your answer with example (PPS_WIN_18)
Because C uses null-terminated strings, which means that the end of any string is marked by the
ASCII value 0 (the null character), which is also represented in C as '\0'.In C, null value is used to
terminate strings so that text based functions can automatically find out end of string and dont have
to be supplied with additional parameter for string size.
Q.3 Describe following string functions in C – Language.
(1) strcpy( ) , (2) strcat( ) , (3) strlen( ) , (4) strcmp ( ) .(CPU_WIN_2019) (CPU_WIN_2017)
(CPU_WIN_2014)(CPU_SUM_2018)

1) strcpy()

This is the string copy function. It copies one string into another string.
Syntax:
strcpy(string1, string2);
The two parameters to the function, string1 and string2, are strings. The function will copy
the
string string1 into the string 1.

2) strcat()

This is the string concatenate function. It concatenates strings.


Syntax:
strcat(string1, string2);
The two parameters to the function, string1 and string2 are the strings to be concatenated.
The
above function will concatenate the string string2 to the end of the string string1.

3) strlen()

This is the string length function. It returns the length of the string passed to it as the
argument.

Syntax:
strnlen(string1)
The parameter string1 is the name of the string whose length is to be determined. The above
function will return the length of the string string1.

4) strcmp()

This is the string compare function. It is used for string comparison.


Syntax:
strcmp(string1, string2);
The above function will return 0 if strings string1 and string2 are similar, less than 0 if

CKPCET, SURAT 2
PPS (3110003)

string1&lt;string2 and greater than 0 if string1&gt;string2.

Q.4 Write a program to copy one string into another and count the number of characters
copied. .(CPU_WIN_2019)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str1[100], str2[100];
int i;
printf("\n\nCopy one string into another string :\n");
printf("-----------------------------------------\n");
printf("Input the string : ");
fgets(str1, sizeof str1, stdin);
/* Copies string1 to string2 character by character */
i=0;
while(str1[i]!='\0')
{
str2[i] = str1[i];
i++;
}
//Makes sure that the string is NULL terminated
str2[i] = '\0';
printf("\nThe First string is : %s\n", str1);
printf("The Second string is : %s\n", str2);
printf("Number of characters copied : %d\n\n", i);
}

CKPCET, SURAT 3
PPS (3110003)

Output:
Copy one string into another string :
-----------------------------------------
Input the string : This is a string to be copied.

The First string is : This is a string to be copied.

The Second string is : This is a string to be copied.

Number of characters copied : 31


Q.5 Write a function which accepts a string and returns the length of the string.
.(CPU_WIN_2019)
#include <stdio.h>
#include <string.h>
int main()
{
char Str[1000];
int i;
printf("Enter the String: ");
scanf("%s", Str);
for (i = 0; Str[i] != '\0'; ++i);
printf("Length of Str is %d", i);
return 0;
}
Output:
Enter the String: hello
Length of Str is 5

CKPCET, SURAT 4
PPS (3110003)

Q.6 Describe array with example. (PPS_WIN_18)


Array is a collection of similar data type. A single variable can hold only one value at a time, If
we want a variable to store more than one value of same type we use array. Array is linear data
structure. It can store fixed number of values.
Syntax of array declaration :
Data-type Array-name[ size of Array ];
Initialization of Array
Initialization means assigning value to declared Array.
int Array [ ] = { 78, 45, 12, 89, 56 };
Examples
#include<stdio.h>
void main()
{
int array [5];
int i;
for(i=0;i<5;i++) //Input arrray from user.
{
printf("\nEnter any number : ");
scanf("%d",&array [i]);
}
for(i=0;i<5;i++) //Output arrray to console.
printf("%d, ",array [i]);
}
Output :
Enter any number : 78
Enter any number : 45
Enter any number : 12
Enter any number : 89
Enter any number : 56 78, 45, 12, 89, 56,

CKPCET, SURAT 5
PPS (3110003)

Q. 7 What is string? Explain with example to find a character from string. (PPS_WIN_18)
A string is a collection of characters, stored in an array followed by null ('\0') character.
#include <stdio.h>
#include<string.h>
int main(void) {
char str[50],c;
int temp=0,i;
printf("\nEnter the string : ");
scanf("%s",str);
printf("\nEnter the char to find : ");
scanf(" %c",&c);
for(i=0;i<strlen(str)&&str[i]!=c;++i);
if(i<strlen(str))
printf("\nCharacter found,pos : %d",i);
else
printf("\nCharacter not found");
return 0;
}
Q.8 What is Array in C language? How is it different from Structure in C language? State
advantages and disadvantages of Array. .(CPU_WIN_2017)

Arrays Structures

1. An array is a collection of related data


1. Structure can have elements of different types
elements of the same type.
2. An array is a derived data type 2. A structure is a programmer-defined data type
3. Any array behaves like a built-in data types. 3. But in the case of structure, first, we have to
All we have to do is to declare an array variable design and declare a data structure before the
and use it. variable of that type are declared and used.
4. Array allocates static memory and uses
4. Structures allocate dynamic memory and uses (.)
index/subscript for accessing elements of the
operator for accessing the member of a structure.
array.
5. An array is a pointer to the first element of it 5. Structure is not a pointer

Advantages
• It is better and convenient way of storing the data of same datatype with same size.
• It allows us to store known number of elements in it.

CKPCET, SURAT 6
PPS (3110003)

• It allows to store the elements in any dimensional array – supports multidimensional array.
Disadvantages
• It allows us to enter only fixed number of elements into it. We cannot alter the size of the
array once array is declared. Hence if we need to insert more number of records than
declared then it is not possible. We should know array size at the compile time itself.
• Inserting and deleting the records from the array would be costly since we add / delete the
elements from the array, we need to manage memory space too.
Q.9 Write a program to check whether a given string is palindrome or not.
.(CPU_WIN_2017)
#include <stdio.h>
#include <string.h>
int main(){
char string1[20];
int i, length;
int flag = 0;
printf("Enter a string:");
scanf("%s", string1);
length = strlen(string1);
for(i=0;i < length ;i++){
if(string1[i] != string1[length-i-1]){
flag = 1;
break;
}
}
if (flag) {
printf("%s is not a palindrome", string1);
}
else {
printf("%s is a palindrome", string1);
}

CKPCET, SURAT 7
PPS (3110003)

return 0;
}
OUTPUT:
Enter a string: wow
wow is a palindrome
Q.10 Discuss initialization of one-dimensional arrays with example(CPU_WIN_2016)
There are four different ways to initialize one-dimensional array in c programming.
1. Initialize array at the time of declaration
One way is to initialize one-dimentional array is to initialize it at the time of declaration. You can
use this syntax to declare an array at the time of initialization.
int a[5] = {10, 20, 30, 40, 50};
2. Initialize all elements of an array with 0 (zero)
If you don’t want to get all elements initialized with garbage, you can initialize them with value 0
(zero). You can do this with the help of this syntax.
int a[5] = {0};
3. Initialize to define the size of an array
it is also possible to define the size of an array by initializing elements of the array. You can use
this syntax for this.
4. Initialize array elements individually
Array is a group of variables, each variable is called element of the array. You can initialize all
elements separately as you initialize any other variable. See following syntax.
int a[5];
a[0] = 10;
a[1] = 20;
a[2] = 30;
a[3] = 40;
a[4] = 50;
Q.11 Write a program to count total words in text. (CPU_WIN_2016)
#include <stdio.h>
#include <string.h>

CKPCET, SURAT 8
PPS (3110003)

void main()
{
char s[200];
int count = 0, i;
printf("Enter the string:\n");
scanf("%[^\n]s", s);
for (i = 0;s[i] != '\0';i++)
{
if (s[i] == ' ' && s[i+1] != ' ')
count++;
}
printf("Number of words in given string are: %d\n", count + 1);
}
OUTPUT:
Enter the string:
hello world
Number of words in given string are: 2
Q.12 Write a program in c for multiplication of two matrix. (CPU_WIN_2015)
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");

CKPCET, SURAT 9
PPS (3110003)

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result

CKPCET, SURAT 10
PPS (3110003)

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
Output:
enter the number of row=3
enter the number of column=3
enter the first matrix element=
111
222
333
enter the second matrix element=
111
222
333
multiply of the matrix=
666
12 12 12
18 18 18
Q.13 Write a program to reverse the input string. (CPU_WIN_2013)
#include <stdio.h>
#include <string.h>

CKPCET, SURAT 11
PPS (3110003)

int main()
{
char s[100];
printf("Enter a string to reverse\n");
gets(s);
strrev(s);
printf("Reverse of the string: %s\n", s);
return 0;
}
OUTPUT
Enter a string to reverse
Computer
Reverse of the string:
retupmoc
Q.14 What is string? Write a program to concatenate two strings without using built in
function. (CPU_WIN_2014)
A string is a sequence of characters terminated with a null character \0 .
#include <stdio.h>
int main()
{
// Get the two Strings to be concatenated
char str1[100] = "Hello", str2[100] = "World";
// Declare a new Strings
// to store the concatenated String
char str3[100];
int i = 0, j = 0;
printf("\nFirst string: %s", str1);
printf("\nSecond string: %s", str2);

CKPCET, SURAT 12
PPS (3110003)

// Insert the first string in the new string


while (str1[i] != '\0') {
str3[j] = str1[i];
i++;
j++;
}
// Insert the second string in the new string
i = 0;
while (str2[i] != '\0') {
str3[j] = str2[i];
i++;
j++;
}
str3[j] = '\0';
// Print the concatenated string
printf("\nConcatenated string: %s", str3);
return 0;
}
Output:
First string: Hello
Second string: World
Concatenated string: HelloWorld

Q. 15 Describe following string functions in C-Language. (CPU_SUM_2014)


What is a string? Explain at least 4 built-in string functions with example(PPS_SUM_2019)
1) strcpy()
This is the string copy function. It copies one string into another string.
Syntax:

CKPCET, SURAT 13
PPS (3110003)

strcpy(string1, string2);
The two parameters to the function, string1 and string2, are strings. The function will copy the
string string1 into the string 1.
2) strcat()
This is the string concatenate function. It concatenates strings.
Syntax:
strcat(string1, string2);
The two parameters to the function, string1 and string2 are the strings to be concatenated. The
above function will concatenate the string string2 to the end of the string string1.
3) strlen()
This is the string length function. It returns the length of the string passed to it as the argument.
Syntax:
strnlen(string1)
The parameter string1 is the name of the string whose length is to be determined. The above
function will return the length of the string string1.
4) strcmp()
This is the string compare function. It is used for string comparison.
Syntax:
strcmp(string1, string2);
The above function will return 0 if strings string1 and string2 are similar, less than 0 if
string1<string2 and greater than 0 if string1>string2.

Q. 16 Write a program in c for multiplication of two matrix. (CPU_SUM_2015)


Write a C program to multiply two N X N Matrix (CPU_SUM_2016)
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);

CKPCET, SURAT 14
PPS (3110003)

printf("enter the number of column=");


scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}

CKPCET, SURAT 15
PPS (3110003)

//for printing result


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
Output:
enter the number of row=3
enter the number of column=3
enter the first matrix element=
111
222
333
enter the second matrix element=
111
222
333
multiply of the matrix=
666
12 12 12
18 18 18

Q. 17 What is string? How they declare and also define null character. (CPU_SUM_2015)
Strings are defined as an array of characters. The difference between a character array and a string
is the string is terminated with a special character '\0'.

CKPCET, SURAT 16
PPS (3110003)

Declaration of strings: Declaring a string is as simple as declaring a one dimensional array. Below
is the basic syntax for declaring a string.
char str_name[size];
In the above syntax str_name is any name given to the string variable and size is used define the
length of the string, i.e the number of characters strings will store. Please keep in mind that there
is an extra terminating character which is the Null character (‘\0’) used to indicate termination of
string which differs strings from normal character arrays.
Null Character: Null character is a character that has all its bits set to 0. A null character, therefore,
has a numeric value of 0, but it has a special meaning when interpreted as text. In some
programming languages, notably C, a null character is used to mark the end of a character string.

Q. 18 Write a program using pointer and function to determine the length of string
(CPU_SUM_2015)
#include<stdio.h>
int main() {
char str[20], *pt;
int i = 0;
printf("Pointer Example Program : Find or Calculate Length of String \n");
printf("Enter Any string [below 20 chars] : ");
gets(str);
pt = str;
while (*pt != '\0') {
i++;
pt++;
}
printf("Length of String : %d", i);
return 0;
}
Output:
Pointer Example Program : Find or Calculate Length of String
Enter Any string [below 20 chars] : FIND-STRING-LENGTH
Length of String : 18

CKPCET, SURAT 17
PPS (3110003)

Q. 19 Explain strcat(),strlen() and strcpy() functions with examples. (CPU_SUM_2017)

strcpy()

This is the string copy function. It copies one string into another string.

Syntax:

strcpy(string1, string2);

The two parameters to the function, string1 and string2, are strings. The function will copy the
string string1 into the string 1.

strcat()

This is the string concatenate function. It concatenates strings.

Syntax:

strcat(string1, string2);

The two parameters to the function, string1 and string2 are the strings to be concatenated. The
above function will concatenate the string string2 to the end of the string string1.

strlen()

This is the string length function. It returns the length of the string passed to it as the argument.

Syntax:

strnlen(string1)

The parameter string1 is the name of the string whose length is to be determined. The above
function will return the length of the string string1.

Example:

#include <iostream>

#include <cstring>

using namespace std;

int main() {

CKPCET, SURAT 18
PPS (3110003)

char name1[10] = "Guru99";

char name2[10] = "John";

char name3[10];

int len;

strcpy(name3, name1);

cout << "strcpy( name3, name1) : " << name3 << endl;

strcat(name1, name2);

cout << "strcat( name1, name2): " << name1 << endl;

len = strlen(name1);

cout << "strlen(name1) : " << len << endl;

return 0;

Output:

Strcpy(name3, name1): Guru99

Strcat(name1,name2): Guru99John

Strlen(name1): 10

Q. 20 What is array? Give example of array & give advantages of array


(CPU_SUM_2017,18)

Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the
same type. An array is used to store a collection of data, but it is often more useful to think of an
array as a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1, ..., and number99, you
declare one array variable such as numbers and use numbers[0], numbers[1], and ...,

CKPCET, SURAT 19
PPS (3110003)

numbers[99] to represent individual variables. A specific element in an array is accessed by an


index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.

#include <stdio.h>

int main () {

int n[ 10 ]; /* n is an array of 10 integers */

int i,j;

/* initialize elements of array n to 0 */

for ( i = 0; i < 10; i++ ) {

n[ i ] = i + 100; /* set element at location i to i + 100 */

/* output each array element's value */

for (j = 0; j < 10; j++ ) {

printf("Element[%d] = %d\n", j, n[j] );

return 0

Output:

Element[0] = 100

Element[1] = 101

CKPCET, SURAT 20
PPS (3110003)

Element[2] = 102

Element[3] = 103

Element[4] = 104

Element[5] = 105

Element[6] = 106

Element[7] = 107

Element[8] = 108

Element[9] = 109

Advantages of Array:

• collection of similar types of data.


• if we want to store the marks of all students it will easy to store in array otherwise we have to
store marks in different different location, which is not easy to memorise.
• we have to only remember the first index of array.
• used to implement other data structure like linked lists,stack,queue, trees, graph etc.
• 2 Dimensional array is used to represent a matrix.

Q. 21 What is string? In how many ways can you accept data in a string? (CPU_SUM_2017)

In C programming, a string is a sequence of characters terminated with a null character \0. For
example:

char c[] = "c string";

When the compiler encounters a sequence of characters enclosed in the double quotation marks, it
appends a null character \0 at the end by default.

Declaring and Initializing a string variable

There are different ways to initialize a character array variable.

char name[13] = "Study"; // valid character array initialization

CKPCET, SURAT 21
PPS (3110003)

char name[10] = {'L','e','s','s','o','n','s','\0'}; // valid initialization

Remember that when you initialize a character array by listing all of its characters separately then
you must supply the '\0' character explicitly.

Some examples of illegal initialization of character array are,

char ch[3] = "hell"; // Illegal

char str[4];

str = "hell"; // Illegal

Q 22 Write a program to accept a string and count the number of vowels present in a string.
(CPU_SUM_2017)

#include <stdio.h>

#include <conio.h>

void main()

char a[100];

int len,i,vow=0;

clrscr();

printf("\nENTER A STRING: ");

gets(a);

len=strlen(a);

for(i=0;i<len;i++)

if(a[i]=='a' || a[i]=='A' || a[i]=='e' || a[i]=='E' || a[i]=='i' || a[i]=='I' || a[i]=='o' || a[i]=='O' ||


a[i]=='u' || a[i]=='U')

vow=vow+1;

CKPCET, SURAT 22
PPS (3110003)

printf("\nTHERE ARE %d VOWELS IN THE STRING",vow);

getch();

Output:

ENTER A STRING: Gaurav

THERE ARE 3 VOWELS IN THE STRINGS

Q. 23 Write a program to find out the largest of an array. (CPU_SUM_2017)

#include <stdio.h>

int main() {

int i, n;

float arr[100];

printf("Enter the number of elements (1 to 100): ");

scanf("%d", &n);

for (i = 0; i < n; ++i) {

printf("Enter number%d: ", i + 1);

scanf("%f", &arr[i]);

// storing the largest number to arr[0]

for (i = 1; i < n; ++i) {

if (arr[0] < arr[i])

arr[0] = arr[i];

CKPCET, SURAT 23
PPS (3110003)

printf("Largest element = %.2f", arr[0]);

return 0;

Output:

Enter the number of elements (1 to 100): 5

Enter number1: 34.5

Enter number2: 2.4

Enter number3: -35.5

Enter number4: 38.7

Enter number5: 24.5

Largest element = 38.70

Q. 24 Explain: 1. Nesting if-else statement (CPU_SUM_2018)


When an if else statement is present inside the body of another “if” or “else” then this is called
nested if else.
Syntax of Nested if else statement:
if(condition) {
//Nested if else inside the body of "if"
if(condition2) {
//Statements inside the body of nested "if"
}
else {
//Statements inside the body of nested "else"
}
}
else {
//Statements inside the body of "else"
}

CKPCET, SURAT 24
PPS (3110003)

Example of nested if..else


#include <stdio.h>
int main()
{
int var1, var2;
printf("Input the value of var1:");
scanf("%d", &var1);
printf("Input the value of var2:");
scanf("%d",&var2);
if (var1 != var2)
{
printf("var1 is not equal to var2\n");
//Nested if else
if (var1 > var2)
{
printf("var1 is greater than var2\n");
}
else
{
printf("var2 is greater than var1\n");
}
}
else
{
printf("var1 is equal to var2\n");
}
return 0;
}
Output:
Input the value of var1:12

CKPCET, SURAT 25
PPS (3110003)

Input the value of var2:21


var1 is not equal to var2
var2 is greater than var1

Q. 25 Write a program in ‘C’ to print the following pattern using loop statement.
(CPU_SUM_2018)
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
#include <stdio.h>
void main()
{
int i,j,rows;

printf("Input number of rows : ");


scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
printf("%d",i);
printf("\n");
}
}
Output:
Input number of rows : 5
1
22
333

CKPCET, SURAT 26
PPS (3110003)

4444
55555

Q. 25 Write a C program to do following task. (CPU_SUM_2019)


(1) Take string from user and print in upper-case
(2) Find string length without using strlen() function.

Take string from user and print in upper-case


#include <stdio.h>
#include <string.h>

int main()
{
char Str1[100];
int i;

printf("\n Please Enter a String that you want to Convert into Uppercase : ");
gets(Str1);

for (i = 0; Str1[i]!='\0'; i++)


{
if(Str1[i] >= 'a' && Str1[i] <= 'z')
{
Str1[i] = Str1[i] -32;
}
}

printf("\n The given String in Upper Case = %s", Str1);

return 0;

CKPCET, SURAT 27
PPS (3110003)

Find string length without using strlen() function.

#include <stdio.h>
int main()
{
/* Here we are taking a char array of size
* 100 which means this array can hold a string
* of 100 chars. You can change this as per requirement
*/
char str[100],i;
printf("Enter a string: \n");
scanf("%s",str);

// '\0' represents end of String


for(i=0; str[i]!='\0'; ++i);
printf("\nLength of input string: %d",i);

return 0;
}
Find string length without using strlen() function.

#include <stdio.h>
int main()
{
/* Here we are taking a char array of size
* 100 which means this array can hold a string
* of 100 chars. You can change this as per requirement
*/

CKPCET, SURAT 28
PPS (3110003)

char str[100],i;
printf("Enter a string: \n");
scanf("%s",str);

// '\0' represents end of String


for(i=0; str[i]!='\0'; ++i);
printf("\nLength of input string: %d",i);

return 0;
}
Q. 27 What is an array? Explain one dimensional and two dimensional array declarations
and initialization with suitable example. (PPS_SUM_2019)
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the
same type. An array is used to store a collection of data .
To declare an array in C, a programmer specifies the type of the elements and the number of
elements required by an array as follows −
type arrayName [ arraySize ];
One-dimensional array :
Conceptually you can think of a one-dimensional array as a row, where elements are stored one
after another.
Syntax: datatype array_name[size];
Example :

int num[100];
float temp[20];
char ch[50];

program :

#include<stdio.h>

int main()
{
int arr[5], i;

CKPCET, SURAT 29
PPS (3110003)

for(i = 0; i < 5; i++)


{
printf("Enter a[%d]: ", i);
scanf("%d", &arr[i]);
}

printf("\nPrinting elements of the array: \n\n");

for(i = 0; i < 5; i++)


{
printf("%d ", arr[i]);
}

// signal to operating system program ran fine


return 0;
}

2-D array
The syntax declaration of 2-D array is not much different from 1-D array. In 2-D array, to declare
and access elements of a 2-D array we use 2 subscripts instead of 1.

Syntax: datatype array_name[ROW][COL];


The total number of elements in a 2-D array is ROW*COL. Let’s take an example.
int arr[2][3];
This array can store 2*3=6 elements. You can visualize this 2-D array as a matrix of 2 rows and 3
columns.
Example :
#include<stdio.h>
#define ROW 3
#define COL 4

int main()
{
int arr[ROW][COL], i, j;
for(i = 0; i < ROW; i++)

CKPCET, SURAT 30
PPS (3110003)

{
for(j = 0; j < COL; j++)
{
printf("Enter arr[%d][%d]: ", i, j);
scanf("%d", &arr[i][j]);
}
}
printf("\nEntered 2-D array is: \n\n");
for(i = 0; i < ROW; i++)
{
for(j = 0; j < COL; j++)
{
printf("%3d ", arr[i][j] );
}
printf("\n");
}
// signal to operating system everything works fine
return 0;
}

MCQ

(CPU_WIN_2019)
Q. int x[5] = {3,4,5}
What will be the value of x[4]?
(a) 0 (b) 4 (c) 5 (d) 3

Q. Which library function is used to compare two strings?

CKPCET, SURAT 31
PPS (3110003)

(a) strstr( ) (b) strchr ( ) (c) strcmp ( ) (d) strlen ( )

CPU_WIN_2013
Q. Which of the following function is more appropriate for reading in a multiword string?
(A) printf(); (B) scanf(); (C) gets(); (D) puts();

CPU_WIN_2014
Q. Which of the following is used as a string termination character?
(a) 0 (b) \0 (c) /0 (d) None of these

CPU_SUM_2016
Q. Array index start at (CPU_SUM_2018)
(a) 1 (b) User Defined (c) 0 (d) None of above

Q. Every string is terminated by NULL character. How it is represented?


(a) ‘\0’ (b) both a and b (c) NULL (d) None of above

CPU_SUM_2017
Q. The format string to accept a string is (CPU_SUM_2018)
(a)%c (b)%d (c)%f (d)%s

Q. Which of the following is used as a string termination character?


(a) 0 (b) \0 (c) /0 (d) None of these

CKPCET, SURAT 32

You might also like