Average of Array

QUESTION

Find the Average of n numbers using array concept. First line of the Input is the number of Input.

“TESTCASE_1”: “5\n45 35 38 31 49\n###—###SEPERATOR—###—\n39”, “TESTCASE_2”: “3\n100 200 300\n###—###SEPERATOR—###—\n200”, “TESTCASE_3”: “2\n15 20\n###—###SEPERATOR—###—\n17”, “TESTCASE_4”: “6\n5 5 5 5 5 5\n###—###SEPERATOR—###—\n5”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct node
{
	int data;
	struct node *next;
}Node;
Node *head;
Node *tr;
Node *prev;
void insert(int n)
{
	Node *temp=(Node*)malloc(sizeof(Node));
	temp->data=n;
	if(head==NULL)
	{
		head=temp;
		head->next=NULL;
	}
	else
	{
	
	tr=head;
	while(tr->next!=NULL)
	{
		tr=tr->next;
	}
	tr->next=temp;
	tr=tr->next;
	tr->next=NULL;
}
}

void display(int n)
{
  long unsigned int sum=0;
	if(head==NULL)
	{
		printf("NULL");
		
	}
	else
	{
		tr=head;
		while(tr->next!=NULL)
		{
          sum=sum+(long unsigned int)tr->data;
			//printf("%d->",tr->data);
			tr=tr->next;
		}
      sum=sum+(long unsigned int)tr->data;
		printf("%ld",sum/n);
	}
}
int main()
{
	int n;

	scanf("%d",&n);
		int data;
		
		//n=strlen(data);
	int i;
	for(i=0;i<n;i++)
	{
		scanf("%d",&data);
		insert(data);
	}
	//deletion();
	display(n);

	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro