CSE Classwork O8
CSE Classwork O8
Classwork
Name : Shamim Ahmmed (Abrar) Submitted to:
Year : 1st year,Even semester Rajshahi University of Engineering and Technology (RUET),
#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