0% found this document useful (0 votes)
17 views5 pages

CSE Classwork O8

This document contains 3 programming assignments involving arrays and pointers in C: 1) Write a program to calculate the sum of elements in an array using pointers. 2) Write a program to find the maximum element in an array using pointers. 3) Write a program to copy one array to another using pointers.

Uploaded by

Goose Bump
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)
17 views5 pages

CSE Classwork O8

This document contains 3 programming assignments involving arrays and pointers in C: 1) Write a program to calculate the sum of elements in an array using pointers. 2) Write a program to find the maximum element in an array using pointers. 3) Write a program to copy one array to another using pointers.

Uploaded by

Goose Bump
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/ 5

CSE-1282

(Computer and Programming language sessional)

Classwork
Name : Shamim Ahmmed (Abrar) Submitted to:

Roll NO : 2002081 Miftahul Mobin Chowdhury Shabdik


Section :B (Lecturer)

Department : Mechanical Engineering Department of Mechanical Engineering,

Year : 1st year,Even semester Rajshahi University of Engineering and Technology (RUET),

Session : 2020-21 Rajshahi,Bangladesh


Date : 26 November,2022
01.you to compute the sum of all elements in an array. The array
elements are 3,4,5,6,7 & 8. Write a C code to find out the sum using
pointers

#include <stdio.h>
#include <malloc.h>

void main()
{ system ("color F4");
int i, n, sum = 0;
int *a;
printf("Enter the size of array \n");
scanf("%d", &n);
a = (int *) malloc(n * sizeof(int));
printf("Enter Elements of the List \n");
for (i = 0; i < n; i++)
{
scanf("%d", a + i);
}
for (i = 0; i < n; i++)
{
sum = sum + *(a + i);
}
printf("Sum of all elements in array = %d\n", sum);
return 0;
}

OUTPUT
02.An array contains several elements. Write a C code to find out the
maximum element of array using pointers.

#include <stdio.h>
#include <stdlib.h>
void findLargest(int* arr, int N)
{ system("color F4");
int i;
for (i = 1; i < N; i++)
{
if (*arr < *(arr + i)) {
*arr = *(arr + i);
}
}
printf("%d ", *arr);
}
int main()
{
int i, N = 4;
int* arr;
arr = (int*)calloc(N, sizeof(int));
if (arr == NULL) {
printf("No memory allocated");
exit(0);
}
*(arr + 0) = 56;
*(arr + 1) = 45;
*(arr + 2) = 5;
*(arr + 3) = 23;
findLargest(arr, N);
return 0;
}

OUTPUT
03.Write a C code to copy an array to another Use pointers.

#include <stdio.h>
#define MAX_SIZE 100
void printArray(int arr[], int size);

int main()
{ system("color F4");
int source_arr[MAX_SIZE], dest_arr[MAX_SIZE];
int size, i;
int *source_ptr = source_arr;
int *dest_ptr = dest_arr;
int *end_ptr;
printf("Enter size of array: ");
scanf("%d", &size);
printf("Enter elements in array: ");
for (i = 0; i < size; i++)
{
scanf("%d", (source_ptr + i));
}
end_ptr = &source_arr[size - 1];
printf("\nstart array before copying: ");
printArray(source_arr, size);
printf("\nDestination array before copying: ");
printArray(dest_arr, size);
while(source_ptr <= end_ptr)
{
*dest_ptr = *source_ptr;
source_ptr++;
dest_ptr++;
}
printf("\n\nStart array after copying: ");
printArray(source_arr, size);
printf("\nDestination array after copying: ");
printArray(dest_arr, size);
return 0;
}
void printArray(int *arr, int size)
{
int i;
for (i = 0; i < size; i++)
{
printf("%d, ", *(arr + i));
}
}
OUTPUT

You might also like