0% found this document useful (0 votes)
3 views9 pages

Exp 5 - Updated

The document contains several C program code snippets that demonstrate the use of pointers and dynamic memory allocation. The programs cover topics such as: 1) Reading and calculating the sum of elements in a 1D array using malloc. 2) Finding the sum of elements by allocating memory using malloc and calloc. 3) Calculating the length of a string using a pointer. 4) Searching for an element in an array using pointers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Exp 5 - Updated

The document contains several C program code snippets that demonstrate the use of pointers and dynamic memory allocation. The programs cover topics such as: 1) Reading and calculating the sum of elements in a 1D array using malloc. 2) Finding the sum of elements by allocating memory using malloc and calloc. 3) Calculating the length of a string using a pointer. 4) Searching for an element in an array using pointers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Write a 

C program to read a one dimensional array and print sum of all elements using Dynamic Memory
Allocation.

int readAndCalculateSum(int *p, int size) {

int i, sum = 0;

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

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

scanf("%d", p + i);

sum = sum + *(p + i);

return sum;

}
Write a program to read and display the elements of an array in reverse order using pointers.

int read(p,n)

{int i;

for (i=0; i<n; i++){scanf("%d", p+i);}

}
Write a program to find the sum of n elements by allocating memory by using malloc() function.

int* allocateMemory(int n) {

return (int *)malloc(n * sizeof(int));

void read(int *p, int n) {

int i;

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

scanf("%d", p + i);

int sum(int *p, int n) {

int i, total = 0;

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

total = total + *(p + i);

return total;

}
Write a program to find the sum of n elements by allocating memory by using calloc() function.

int* allocateMemory(int n) {

return (int *)calloc(n, sizeof(int));

void read(int *p, int n) {

int i;

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

scanf("%d", p + i);

int sum(int *p, int n) {

int i, total = 0;

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

total = total + *(p + i);

return total;

}
Write a program in C to Calculate the length of the string using a pointer.

#include <stdio.h>

int calculateLength(char*);

void main()

char str1[100];

int l;

fgets(str1, sizeof str1, stdin);

l = calculateLength(str1);

printf("%d",l-1);

int calculateLength(char* ch)

int ctr = 0;

while (*ch != '\0')

ctr++;

ch++;

return ctr;

}
Program to search an element in an array using pointers
#include <stdio.h>
#define MAX_SIZE 100
void inputArray(int * arr, int size);
int search(int * arr, int size, int toSearch);
int main()
{
int array[MAX_SIZE];
int size, toSearch, searchIndex;
scanf("%d", &size);
inputArray(array, size);
scanf("%d", &toSearch);
searchIndex = search(array, size, toSearch);
if(searchIndex == -1)
printf("-1");
else
printf("1");
return 0;
}
void inputArray(int * arr, int size)
{
int * arrEnd = (arr + size - 1);
while(arr <= arrEnd)
{
scanf("%d", arr++);
}
}

int search(int * arr, int size, int toSearch)


{
int index = 0;
int * arrEnd = (arr + size - 1);
while(arr <= arrEnd && *arr != toSearch) {
arr++;
index++;
}
if(arr <= arrEnd)
return index;

return -1;
}
Program to find the number of prime numbers and number of composite numbers in an array
to which memory is allocated dynamically using pointers.

#include<stdio.h>
#include<stdlib.h>
int checkPrime(int num) {
if(num == -1) {
return 0;
}
for(int i = 2; i <= num/2; i++) {
if(num % i == 0) {
return 0;
}
}
return 1;
}
void main() {
int n, i, *ptr, primeCount = 0, compCount = 0;
scanf("%d", &n);
ptr=(int*)malloc(n*sizeof(int));
for(i = 0;i < n;++i)
{
scanf("%d",ptr+i);
}
for(i = 0; i < n; i++) {
if(checkPrime(*(ptr+i))) {
primeCount++;
} else {
compCount++;
}
}
printf("%d\n%d\n", primeCount, compCount);
}
Write a program to read and display the elements of an array using pointers.

void read(int *p, int n) {


int i;
for (i = 0; i < n; i++) {
scanf("%d", p + i);
}
}
void display(int *p, int n) {
int i;
for (i = 0; i < n; i++) {
printf("%d ", *(p + i));
}
}
Write a C program using pointers to compute the sum, mean and standard deviation of all elements sorted in
an array of n real numbers.

#include <stdio.h>
#include <math.h>
void main() {
float a[10], *ptr, mean, std, sum = 0, sumstd = 0;
int n, i;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%f", &a[i]);
}
ptr = a;
for (i = 0; i < n; i++) {
sum = sum + *ptr;
ptr++;
}
mean = sum / n;
ptr = a;
for (i = 0; i < n; i++) {
sumstd = sumstd + pow((*ptr - mean), 2);
ptr++;
}
std = sqrt(sumstd / n);
printf("%f\n", sum);
printf("%f\n", mean);
printf("%f\n", std);
}

You might also like