0% found this document useful (0 votes)
5 views7 pages

Array

The document provides a comprehensive overview of arrays in C programming, including definitions, declarations, initializations, and various operations such as insertion, printing, searching, and replacing elements. It explains concepts like contiguous storage, homogeneous data, and the use of loops for handling arrays. Additionally, it includes example C programs demonstrating how to manipulate arrays and perform calculations like summation of even numbers and average marks of students.

Uploaded by

gautam_me
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)
5 views7 pages

Array

The document provides a comprehensive overview of arrays in C programming, including definitions, declarations, initializations, and various operations such as insertion, printing, searching, and replacing elements. It explains concepts like contiguous storage, homogeneous data, and the use of loops for handling arrays. Additionally, it includes example C programs demonstrating how to manipulate arrays and perform calculations like summation of even numbers and average marks of students.

Uploaded by

gautam_me
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

ARRAY

Q. Define array.
Answer: An array is a contiguous memory location containing similar types of data. Each memory location is
referred by a common name but differentiated by index number.
[Link] is index?
Answer: The locations of each element in an array referred by integer constants starting from zero is called
index.
[Link] is homogeneous data?
Answer: Array contains similar types of data is called homogeneous data.
[Link] is contiguous storage?
Answer: Contiguous storage is a file/data organization technique that places blocks of records into physically
adjacent memory block. It's an efficient way to store data in continuous blocks, allowing for faster retrieval of
specific blocks.
Q. How to declare an array in C?
Answer:
To declare an array, we have to define the data type (like int) and specify the name of the array followed by size
of the array in integer constant inside square brackets [] and terminated by semicolon..
The syntax is as follows: Datatype <arrayname>[max size];
e.g. int a[5];
Q. How to initialize an array in C program?

Answer: To initialize an array, we need to define the data type (like int) and specify the name of the array
followed by size of the array in integer constant inside square brackets [].To insert values to it, we use a comma-
separated list, inside curly braces. In initialization, mentioning the size of the array is optional.

The syntax is as follows: Datatype <arrayname>[max size]={value1, value2,….valuen};

OR

Datatype <arrayname>[ ]={value1, value2,….valuen};

e.g. int myNumbers[] = {25, 50, 75, 100};

Q. How to insert and print array elements?

Answer: we can insert and print elements of array using two methods.

a) Without using loop: If the size of the array is small that an user can handle easily the we can perform
insert and print operation without using loop. E.g.

scanf(“%d”,&a[0]);
scanf(“%d”,&a[1]);
scanf(“%d”,&a[3]);
scanf(“%d”,&a[2]);

printf(“%d”,a[0]);
printf(“%d”,a[1]);
printf(“%d”,a[3]);
printf(“%d”,a[2]);

In this method, we can also perform input and print operation in/from random location.

b) Using loop: If the size of the array is large that an user cannot handle easily the we can perform insert
and print operation using loop. E.g.

for(i=0;i<=size; i++)
{ scanf(“%d”,&a[i]);}

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

{ printf(“%d”,a[i]);}

Q. How can an element be search in an array?


Answer: A straightforward search strategy used to locate a given element in an array or list is called linear
search, sometimes referred to as sequential search. It works by iterating through the array one element at a
time until the desired element is found. If the element is not found, the search will return -1

Q. Why do we use array in computer program?

Answer: Arrays are used to store multiple values in a single variable, instead of declaring separate variables for
each value. it is also used in Implementing data structures such as vectors, lists, stacks, queues, and matrices.
Arrays can be used to implement CPU scheduling algorithms.

Q. Can we store both integer and float datatypes of data in a single array? Demonstrate by writing a
simple C program.

Answer: No, we cannot store both in integer and float datatypes of data in a single array. because array is a
contiguous memory location contains similar types of data.

e.g if declare of an array of type integer then all the elements of the array must be integer type.

#include <stdio.h>

int main() {

int values[5];

printf("Enter 5 integers: ");

// taking input and storing it in an array


for(int i = 0; i < 5; ++i) {
scanf("%d", &values[i]);
}

printf("Displaying integers: ");

// printing elements of an array


for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}

Q. Write the indices of the first and last element of the following array declaration.
char city[7]={‘S’,’I’,’L’,’C’,’H’,’A’,’R’};

Answer: Indices of the first element of the array is 0.


Indices of the last element of the array is 6.

Q. Write a C program and declare an integer type array with capacity 7. Take input to the array
from the keyboard. Display the 8th element of the array. Hint: display num[7] if num is the name of
the array. Analyse the output.

Answer: This program will take input for an array of size 7 and try to display the 8th element. However, as
arrays in C program are 0-indexed, there is no 8th element in an array of size 7, so this will likely result in
undefined behaviour.
#include <stdio.h>

int main() {

int array[7]; // Declare an integer array with capacity 7


int i;
// Input values into the array from the keyboard
printf("Enter 7 integers, one at a time:\n");
for (i = 0; i < 7; i++) {
scanf("%d", &array[i]);
}
// Display the 7th element of the array (index 6)
if (i >= 7) {
printf("The 7th element is: %d\n", array[6]);
} else {
printf("You haven't entered enough elements to display the 7th element.\n");
}
return 0;
}

Q. Write a C program and declare an integer type array with 7 elements in it. Display the address of
the individual elements in the array.
Answer:
#include <stdio.h>
int main() {
int num[7];
for(int i = 0; i < 7; i++) {
printf(“Address of element %d: %p\n”, i, &num[i]);
}
return 0;
}

Q. Write a C program and declare two integer type arrays, each with capacity 7. Take input only to
the first array. Write a loop to copy the elements of the first array to the second one. Display the
elements of the second array.
Answer:

#include <stdio.h>

int main() {

int array1[7], array2[7];


int i;
printf("Enter 7 integers, one at a time:\n");
for (i = 0; i < 7; i++) {
scanf("%d", &array1[i]);
}
for (i = 0; i < 7; i++) {
array2[i]= array1[i];
}

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


printf("%d", &array2[i]);
}
return 0;
}
Q. Write a strategy to find the summation of all the even numbers stored in an array. Write a C
program for the same.
Answer:
Strategy:
• Step 1: Declare an integer array to store the numbers.
• Step 2: Initialize a variable to hold the sum, initially set to 0.
• Step 3: Use a loop to iterate through the array.
• Step 4: For each element in the array, check if it’s an even number.
• Step 5: If the element is even, add it to the sum.
• Step 6: Continue this process for all elements in the array.
• Step 7: After the loop, the sum will contain the summation of all even numbers.

#include <stdio.h>
int main() {

int array[] = {2, 5, 8, 10, 15, 6, 12, 7}; // Example array


int sum = 0;
for (int i = 0; i < 8; i++) {

if (array[i] % 2 == 0) {

sum += array[i];
}
}
printf("The summation of even numbers in the array is: %d\n", sum);
return 0;
}

Q. Write a strategy to the summation of all the even positioned numbers stored in an array. Write a
C program for the same.
Answer:

Strategy:
• Step 1: Declare an integer array to store the numbers.
• Step 2: Initialize a variable to hold the sum, initially set to 0.
• Step 3: Use a loop to iterate through the array, starting from the first even index (index 1).
• Step 4: For each even-positioned element in the array, add it to the sum.
• Step 5: Continue this process for all even-positioned elements in the array.

#include <stdio.h>
int main() {
int num[] = {1, 2, 4, 3, 5, 6, 7, 7, 8};
int sum = 0;
for(int i = 0; i < 9; i++) {
if(i % 2 != 0) {
sum += num[i];
}
}
printf(“Sum of numbers at even positions: %d\n”, sum);
return 0;
}
Q. Write the logic to replace only the first occurrence of an element in an array. For example, if the
array elements are {1, 2, 3, 4, 5, 1, 2, 3) and we want to replace element 3 by 0, the array content
becomes {1,2, 0, 4, 5, 1, 2, 3). Write a complete C program incorporating the logic.

Answer:
Logic of the program:
• Declare an integer array to store the numbers.
• Declare variables to store the element you want to replace (e.g., ‘elementToReplace’) and the
replacement value (e.g., ‘replacementValue’).
• Use a loop to iterate through the array.
• When you find the first occurrence of the element you want to replace, replace it with
the ‘replacementValue’.
• Exit the loop after the replacement is made.
• Continue the loop for the remaining elements in the array.
• Print the updated array.
#include <stdio.h>
int main() {

int array[] = {1, 2, 3, 4, 5, 1, 2, 3}; // Example array


int elementToReplace = 3;
int replacementValue = 0;
int i;
// Find and replace the first occurrence
for (i = 0; i < 8; i++)
{
if (array[i] == elementToReplace)
{
array[i] = replacementValue;
break; // Exit the loop after replacement
}
}
// Print the updated array
printf("Updated array: ");
for (i = 0; i < 8; i++) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}

[Link] the logic to replace only the last occurrence of an element in an array. For example, if the
array elements are {1, 2, 3, 4, 5, 1, 2, 3} and we want to replace element 3 by 0, the array content
becomes {1, 2, 3, 4, 5, 1, 2, 0}. Write the complete C program incorporating the logic.
Solution:
Logic:
● Create a new array that will store numbers in a array.
● Use the for loop to iterate over the array.
● Enter the number to be searched and replaced.
● Use the for loop to search for the number, if it encountered the number, store the index of the number to a
variable.
● So, the variable will store the last index number of searched array.
● Replace the last index number in the variable with 0.
● Display the changed array.

#include <stdio.h>

int main() {

int a[7], i, s, flag = -1; // Initialize flag to -1


printf("Enter the values of the array: ");
for (i = 0; i < 7; i++) {
scanf("%d", &a[i]);
}
printf("\nEnter the number to be replaced: ");
scanf("%d", &s);
for (i = 0; i < 7; i++) {
if (a[i] == s) {
flag = i; // Update the flag to store the index of the first occurrence
break; // Exit the loop after finding the first occurrence
}
}
if (flag != -1) {
a[flag] = 0;
printf("\nThe new array after replacing the first element is:\n");
for (i = 0; i < 7; i++) {
printf("%d ", a[i]);
}
printf("\n");
} else {
printf("\nThe element to be replaced was not found in the array.\n");
}
return 0;
}

[Link] a C program to replace all the even positioned elements in an integer array by 0. For
example,if the array elements are {1, 2, 3, 9, 5, 5, 7, 1, 9), it becomes {1, 0, 3, 0, 5, 0, 7, 0, 9}.
Answer:
#include <stdio.h>
int main() {
int num[] = {1, 2, 3, 9, 5, 5, 7, 1, 9};
for(int i = 0; i < 9; i++) {
if(i % 2 != 0) {
num[i] = 0;
}
}
for(int i = 0; i < 9; i++) {
printf(“%d “, num[i]);
}
return 0;
}

[Link] a strategy to replace all the odd numbers in an integer array by 0. For example, if the
arrayelements are {1, 2, 3, 9, 5, 5, 7, 1, 9}, it becomes (0, 2, 0, 0, 0, 0, 0, 0, 0). Write a C program for the
same.
Answer:

Strategy:

• Step1: Declare an integer array to store the numbers.


• Step 2: Use a loop to iterate through the array.
• Step3: For each element in the array, check if it’s an odd number (i.e., the remainder when dividing by 2
is not 0).
• Step4: If the element is odd, replace it with 0.
• Step5: Continue this process for all elements in the array.

#include <stdio.h>
int main() {
int array[] = {1, 2, 3, 9, 5, 5, 7, 1, 9}; // Example array
int n = sizeof(array) / sizeof(array[0]); // Calculate the number of elements
// Replace all odd numbers with 0
for (int i = 0; i < n; i++) {
if (array[i] % 2 != 0) {
array[i] = 0;
}
}

// Print the updated array


printf("Updated array: ");
for (int i = 0; i < n; i++) {

printf("%d ", array[i]);


}
printf("\n");
return 0;
}

Q. Write a C program to declare 3 integer type arrays to store the marks of 10 students scored in 3
different subjects. Take another integer to store the average marks of the students. The average
mark of a student is the average of the marks scored by the student in 3 subjects. This should be
calculated and inserted by the program. Then you should display the average marks of the
students. The program should also display “PASS” or “FAIL” along with the average as per the
rule: PASS if average > = 45, FAIL otherwise.

Answer:

#include <stdio.h>
int main() {
int marks1[10], marks2[10], marks3[10], average[10];
for(int i = 0; i < 10; i++)
{
printf(“Enter marks for student %d in 3 subjects: “, i+1);
scanf(“%d %d %d”, &marks1[i], &marks2[i], &marks3[i]);
average[i] = (marks1[i] + marks2[i] + marks3[i]) / 3;
printf(“Average marks for student %d: %d “, i+1, average[i]);
if(average[i] >= 45) {

printf(“(PASS)\n”);
} else {
printf(“(FAIL)\n”);
}
}
return 0;
}

You might also like