0% found this document useful (0 votes)
9 views2 pages

C Prgrms

The document contains three programming tasks: the first task involves writing a C program to find the largest element in an array using dynamic memory allocation; the second task requires creating five linked lists using a custom node structure; the third task is to implement a simple calculator that performs addition, subtraction, multiplication, and division using function pointers.

Uploaded by

sumavarshithak5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

C Prgrms

The document contains three programming tasks: the first task involves writing a C program to find the largest element in an array using dynamic memory allocation; the second task requires creating five linked lists using a custom node structure; the third task is to implement a simple calculator that performs addition, subtraction, multiplication, and division using function pointers.

Uploaded by

sumavarshithak5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Write a program to find largest element using dynamic memory allocation (malloc)

#include <stdio.h>
#include <stdlib.h>

int main()
{
int *arr, n, i;
int largest;
printf("Enter the number of elements: ");
scanf("%d", &n);
arr = (int *)malloc(n * sizeof(int));
if (arr == NULL)
{
printf("Memory allocation failed!\n");
return -1;
}
printf("Enter the elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
largest = arr[0];
for (i = 1; i < n; i++)
{
if (arr[i] > largest)
{
largest = arr[i];
}
}
printf("The largest element is: %d\n", largest);
free(arr);
return 0;
}

2. Create 5 linked lists

typedef struct node


{
void* dataptr;
struct node* link;
}NODE;

NODE* createnode (void* itemptr)


{int NULL;
NODE* nodeptr;
nodeptr=(NODE*)malloc(sizeof(NODE));
nodeptr->dataptr=itemptr;
nodeptr->link=NULL;
return nodeptr;
}

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int* newdata;
int* nodedata;
NODE* node;
newdata=(int*)malloc(sizeof(int));
*newdata=2;
node=createnode(newdata);
newdata=(int*)malloc(sizeof(int));
*newdata=100;
node->link=createnode(newdata);
newdata = (int*)malloc(sizeof(int));
*newdata = 45;
node->link->link = createnode(newdata);
newdata = (int*)malloc(sizeof(int));
*newdata = 65;
node->link->link->link = createnode(newdata);
newdata = (int*)malloc(sizeof(int));
*newdata = 35;
node->link->link->link->link = createnode(newdata);
nodedata=(int*)node->dataptr;
printf("data from node 1:%d\n",*nodedata);
nodedata=(int*)node->link->dataptr;
printf("data from node 2:%d\n",*nodedata);
nodedata=(int*)node->link->link->dataptr;
printf("data from node 3:%d\n",*nodedata);
nodedata=(int*)node->link->link->link->dataptr;
printf("data from node 4:%d\n",*nodedata);
nodedata=(int*)node->link->link->link->link->dataptr;
printf("data from node 5:%d\n",*nodedata);
return 0;
}

3. Write a program to create simple calculator having operations add, sub, multiply
and divide using function pointers

You might also like