0% found this document useful (0 votes)
14 views10 pages

practical 1

The document contains several C programs demonstrating array manipulation techniques, including updating elements in one-dimensional arrays, increasing and decreasing the size of arrays, and working with subarrays in both one-dimensional and two-dimensional arrays. It explains how to allocate and reallocate memory for arrays, copy elements between arrays, and modify elements within specified subarray ranges. Additionally, it provides examples of printing array contents and managing memory effectively.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
14 views10 pages

practical 1

The document contains several C programs demonstrating array manipulation techniques, including updating elements in one-dimensional arrays, increasing and decreasing the size of arrays, and working with subarrays in both one-dimensional and two-dimensional arrays. It explains how to allocate and reallocate memory for arrays, copy elements between arrays, and modify elements within specified subarray ranges. Additionally, it provides examples of printing array contents and managing memory effectively.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

Update array element using One Dimensional Array in C

#include<stdio.h>

int main()

int i,t,a[10],n,m,s,j=0,b[10];

printf("\nEnter the Limit:");

scanf("%d",&n);

printf("\nEnter the Values:");

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

scanf("%d",&a[i]);

printf("\nGiven values are:");

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

printf("\na[%d]=%d",i,a[i]);

printf("\nEnter the position to be update:");

scanf("%d",&t);

printf("\nEnter the value to be update:");

scanf("%d",&s);

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

if(i==t)

a[i]=s;

printf("\nUpdated value is:");

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

printf("\na[%d]=%d",i,a[i]);

return 0;

}
Increase The Size Of An Array In C

#include<stdio.h>

#include<stdlib.h>

int main(){

//Two pointers for two different arrays

int *p;

int *q;

//declaring array at pointer p

p = (int *)malloc(5*sizeof(int));

p[0]=1;

p[1]=3;

p[2]=5;

p[3]=7;

p[4]=9;

//Printing the elements of p

printf("Array p: \n");

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

printf("%d \n",p[i]);

//declaring array at pointer q

q=(int *)malloc(7*sizeof(int));

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

q[i]=p[i];//assigning elements of p to q

free(p);//releasing the memory held by pointer p

p=q; //assigning the address held by q to p for the array

q=NULL; //removing the address of array from q


//printing the elements of p

printf("Array q converted to p: \n");

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

printf("%d \n",p[i]);

return 0;

1.Create an array (lets say p) with n items.


2.Create another array (lets say q) but an empty one which is larger than array p.
3.Now copy the elements of p into q by a simple for loop.
4.Delete the memory held by pointer p using free(p); so that array p no longer exists.
5.Assign the address of array q in p.
6.Assign q the value NULL so that it can't access array q.
7.And that's it. The array size is now increased.
#include<stdio.h>

int main ()

int arr[5]={34,65,78,23,52};

int i;

int *pr;

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

pr=&arr[i];

printf(“\n Address of Array[%d]=%d” ,i ,pr);

pr++;

return 0;

Subarray/Substring

A subarray is a contiguous part of the array. An array that is inside another array. For example, consider
the array [1, 2, 3, 4], There are 10 non-empty sub-arrays. The subarrays are (1), (2), (3), (4), (1,2), (2,3),
(3,4), (1,2,3), (2,3,4) and (1,2,3,4). In general, for an array/string of size n, there are n*(n+1)/2 non-
empty subarrays/substrings.

• A subarray must be contiguous, meaning there can't be any gaps between its elements.

• The original array is also considered a subarray of itself.

• A single element can be considered a subarray.

How to work with subarrays in C:

• You can access elements of a subarray using the same indexing as the original array.

• You can often iterate over subarrays using loops, adjusting the starting and ending indices to
define the desired subarray.
Write a c program on 2D array to Increase & Decrease No of subarrays

#include <stdio.h>

#include <stdlib.h>

// Function to print the 2D array

void printArray(int **array, int rows, int cols) {

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

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

printf("\n");

printf("\n");

int main() {

int rows = 3, cols = 3; // Initial dimensions

// Allocate memory for the 2D array

int **array = (int **)malloc(rows * sizeof(int *));

for (int i = 0; i < rows; i++) {

array[i] = (int *)malloc(cols * sizeof(int));

// Initialize the array with some values

int value = 1;

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

array[i][j] = value++;

}
}

printf("Original 2D Array: \n");

printArray(array, rows, cols);

// Increase the number of subarrays (rows)

int newRows = 5;

array = (int **)realloc(array, newRows * sizeof(int *));

for (int i = rows; i < newRows; i++) {

array[i] = (int *)malloc(cols * sizeof(int));

for (int j = 0; j < cols; j++) {

array[i][j] = 0; // Initialize new rows with 0

rows = newRows;

printf("After Increasing Rows:\n");

printArray(array, rows, cols);

// Decrease the number of subarrays (rows)

newRows = 2;

for (int i = newRows; i < rows; i++) {

free(array[i]); // Free memory of removed rows

array = (int **)realloc(array, newRows * sizeof(int *));

rows = newRows;

printf("After Decreasing Rows:\n");

printArray(array, rows, cols);

// Free the remaining memory


for (int i = 0; i < rows; i++) {

free(array[i]);

free(array);

return 0;

}
Write a c program on 2D array to Increase & Decrease elements in the subarrays

#include <stdio.h>

#define ROWS 3

#define COLS 4

void printArray(int arr[ROWS][COLS]) {

for (int i = 0; i < ROWS; i++) {

for (int j = 0; j < COLS; j++) {

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

printf("\n");

printf("\n");

void modifySubarray(int arr[ROWS][COLS], int startRow, int endRow, int startCol, int endCol, int
value) {

for (int i = startRow; i <= endRow; i++) {

for (int j = startCol; j <= endCol; j++) {

arr[i][j] += value; // Increase or decrease by the given value

int main() {

int array[ROWS][COLS] = {

{1, 2, 3, 4},
{5, 6, 7, 8},

{9, 10, 11, 12}

};

printf("Original Array:\n");

printArray(array);

// Increase elements in a subarray

printf("Increasing elements in subarray (row 0-1, col 1-2) by 5:\n");

modifySubarray(array, 0, 1, 1, 2, 5);

printArray(array);

// Decrease elements in a subarray

printf("Decreasing elements in subarray (row 1-2, col 2-3) by 3:\n");

modifySubarray(array, 1, 2, 2, 3, -3);

printArray(array);

return 0;

You might also like