Anda di halaman 1dari 10

Ex No: 5

One Dimensional Arrays

Aim: To develop programs that illustrates the usage of One Dimensional Arrays.
1. Read 10 elements of integer type using One Dimensional Array and replace all even elements by 0
and Odd by 1.
Algorithm:
START
Declare variables arr[20],i
Step 1: Read the array values:
Step 2: Loop from i=0 to n
arr[i] value to 0
arr[i+1] values to 1
Step 3: Print array elements
STOP
INPUT:
Enter the Number of array elements: 10
10 22 32 65 81 12 28 45 63 87
OUTPUT:
0
1
0
1
0
1
0
1
0
1
2. Find the first repeated element in a One Dimensional Array.
Algorithm:
START
Declare variables arr[20],i,j,n,count,ind,ele
Step 1: Read Number of array elements
Step 2: Read the array values:
Step 3: Loop from i=0 to n
Step 4: Initialize ind = -1
Step 5: Loop from j=i+1 to n
if(arr[i] is equal to arr[j])
ele=arr[j]
ind=j
count++
break the loop
Step 6: check if ind value not equals to -1
Print ele is repeated at the index ind
Step 7: check if count is equal to 0
Print There is no repeating element
STOP
INPUT:
Enter the Number of array Elements: 6
Enter Element 1: 22
Enter Element 2: 54
Enter Element 3: 29
Enter Element 4: 22
Enter Element 5: 98
Enter Element 6: 29
OUTPUT:
22 repeated at index 3
29 repeated at index 5
3. A class of N students sits in 2 class rooms A and B in increasing order of their roll numbers. The
invigilators collect the answer scripts in ascending order and give them to Prof Siva who handles
the subject. Help Prof Siva in arranging all the answer scripts in ascending order of the students’
roll numbers.
Algorithm:
START
Declare variables arr1[50], arr2[50],arr3[100],i,j,sizea,sizeb,temp
Step 1: Enter the total number of students in class room 'A' and roll numbers
Step 2: Read the arr1 values
Step 3: Enter the total number of students in class room 'B' and roll numbers
Step 4: Read the arr2 values
Step 5: Loop from i=0 to sizea
arr3[i]=arr1[i]
Step 6: Loop from i=sizea,j=0 to i<sizea+sizeb,j<sizeb
arr3[i]=arr2[i]
Step 7: Loop from i=0 to sizea+sizeb
Loop from j=i+1 to sizea+sizeb
check arr3[i] is greater than arr3[j]
true swap arr3[i] and arr3[j]
Step 8: Print arr3 array
STOP
INPUT:
Enter the total number of students in class room 'A': 6
Enter 6 students' Roll numbers :
Enter Roll number of student 1 : 13
Enter Roll number of student 2 : 34
Enter Roll number of student 3 : 23
Enter Roll number of student 4 : 35
Enter Roll number of student 5 : 19
Enter Roll number of student 6 : 27
Enter the total number of students in class room 'B': 7
Enter 7 students' Roll numbers :
Enter Roll number of student 1 : 84
Enter Roll number of student 2 : 46
Enter Roll number of student 3 : 58
Enter Roll number of student 4 : 69
Enter Roll number of student 5 : 72
Enter Roll number of student 6 : 81
Enter Roll number of student 7 : 53
OUTPUT:
Order of arranged answer scripts[13 students]:
13
19
23
27
34
35
46
53
58
69
72
81
84
4. For this problem, you must allow the user to enter exactly 5 numbers as initial input. The program
must then ask for an additional number. The output of the program will indicate whether the last
number is contained in the first 5 numbers.
Algorithm:
START
Declare variables arr[20], i,n,item,pos
Step 1: Enter the size of array and array elements
Step 2: Read the values of array elements
Step 3: Enter an item to find
Step 4: Loop from i=0 to n
Check each element with the item to be searched
Step 5: Print the position
STOP
INPUT:
Enter size of an Array :6
Enter elements of Array:
Enter element 1:24
Enter element 2:56
Enter element 3:34
Enter element 4:12
Enter element 5:78
Enter element 6:65
Enter an item to find :12
OUTPUT:
12 found @ position 4.
5. Crack the Code

1. What will be output if you will execute following c code?


#include<stdio.h>
main()
{
int i;
for(i=0;i<5;i++)
{
int i=10;
printf(" %d",i);
i++;
}
}

2. The following program is intended to compute 5! = 120, but it prints 24 instead. What's
wrong with it?

main ( )
{
int theNum, total;
total = 1;
theNum = 5;
while (theNum > 1)
{
total *= --theNum;
}
printf ("%d", total);
return 0;
}
3. Fill in the blanks in the code below to convert the while loop in “ qtn” to an
equivalent for loop.

qtn:
main ( )
{
int num, total;
total = 1;
while(num>1)
{
total *= num;
}
printf ("%d", total);

main ( )
{
int num, total;
total = 1;
for ( ____ ; _____ ; _____ )
{
total *= num;
}
printf ("%d", total);
return 0;
}

4. Fill in the blank in the code below so that it displays the element in reverse
. #include <stdio.h>
main()
{
int arr[5] = {10,20,30,40,50},i;
for(________;_______;_________)
printf(______________________);
}
5. Fill in the blank in the code below so that it prints size of array and all array elements.
#include"stdio.h"
main()
{
int arr[5] = {10,20,30,40,50},i;
printf("Array size is: %d", ________);
________;
printf(“Array Elements are: \n”);
while(_________)
{
printf(“_____”,________);
_________;
}
}

6. Match the following.


gcc compiler:
char c[8]= “infinite”; 32
printf(“%d”, sizeof(c));

gcc compiler:
Segmentation
int c[8]= {32,40,20};
Fault
printf(“%d”, sizeof(c));

int c[8]= {32,40,20};


10
printf(“%d”, c[5]);

int MAX = 10;


int arr[MAX]; 8
printf(“%d”, sizeof(arr));

gcc compiler:
int c[8]= {0}; Compiler Error
printf(“%s”, sizeof(c));

0
7. Choose the correct answer?
#include <stdio.h>
int main()
{
int var[5]={0};
int count=0;
var[++count]=++count;
for(count=0;count<5;count++)
printf("%d ",var[count]);
return 0;
}
A. 0 1 0 0 0
B. 0 2 0 0 0
C. 0 0 2 0 0
D. 0 0 0 0 0

8. What will be output of the following c program?


#include<stdio.h>
void main(){
int xx[10]={5};
printf("%d %d",xx[1],xx[9]);
}

9. What is the output of the following code?

main()
{
int a[4] = {1,4};
printf(“%d”,a[3]);
}
10. What will happen if in a C program you assign a value to an array element whose
subscript exceeds the size of array?

A. The element will be set to 0.

B. The compiler would report an error.

C. The program may crash if some important data gets overwritten.

D. The array size would appropriately grow.

Anda mungkin juga menyukai