Professional Documents
Culture Documents
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
int* arr = new int[n];
int sum = 0;
for(int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
sort(arr, arr + n);
double median;
double mean = sum / (double) n;
if(n % 2 == 0) {
int n1 = arr[n / 2 - 1];
int n2 = arr[n / 2];
median = (n1 + n2) / 2.0;
}
else {
median = arr[n / 2];
}
cout << "Mean is - " << mean << endl << "Median is - " << median;
return 0;
}
Output-
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
int arr[100];
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
int choice;
cin >> choice;
int k, i;
switch (choice) {
case 1:
cout << "Enter number to be added and its index";
cin >> k;
cin >> i;
for(int j = 99; j > i; j--) {
arr[j] = arr[j - 1];
}
arr[i] = k;
n++;
break;
case 2:
cout << "Enter number to be deleted";
cin >> k;
int j;
for(j = 0; j < n; j++) {
if(arr[j] == k) {
break;
}
}
for(int l = j; l < 100; l++) { Outputs-
arr[l] = arr[l + 1];
}
n--;
break;
default:
cout << "Invalid choice";
break;
}
return 0;
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
int* arr = new int[n];
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
int k;
cin >> k;
delete [] arr;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int* arr = new int[n];
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
return 0;
}
Output-
int main() {
int n1, n2;
priority_queue<int, vector<int>, greater<int> > pq;
cin >> n1;
int* arr1 = new int[n1];
for(int i = 0; i < n1; i++) {
cin >> arr1[i];
pq.push(arr1[i]);
}
cin >> n2;
int* arr2 = new int[n2];
for(int i = 0; i < n2; i++) {
cin >> arr2[i];
pq.push(arr2[i]);
}
return 0;
}
Output-
#include <iostream>
using namespace std;
int main() {
int marks[10][5];
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 10; j++) {
cout << "Marks of course " << i + 1 << " of student " << j + 1 << " ";
cin >> marks[i][j];
}
}
return 0;
}
Outputs-
#include <iostream>
struct Node{
int num;
Node *next;
};
void display(){
if(head==NULL){
cout<<"List is empty!"<<endl;
return;
}
struct Node *temp=head;
while(temp!=NULL){
cout<<temp->num<<" ";
temp=temp->next;
}
cout<<endl;
}
display();
insertNode(10);
insertNode(20);
insertNode(30);
insertNode(40);
insertNode(50);
display();
deleteItem(); deleteItem(); deleteItem(); deleteItem(); deleteItem();
deleteItem();
display();
return 0;
}
-To print elements of linked list in reverse order without disturbing it.
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node* next;
};
void printReverse(Node* head)
{
if (head == NULL)
return;
printReverse(head->next);
cout << head->data << " ";
}
void push(Node** head_ref, char new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
Node* head = NULL;
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
printReverse(head);
return 0;
}
#include <iostream>
using namespace std;
struct Node{
int data;
Node *link;
};
Node *head=NULL;
void reverseList()
{
Node *p,*c,*n;
p=NULL;
c=head;
while(c!=NULL)
{
n=c->link;
c->link=p;
p=c;
c=n;
}
head=p;
}
void insertEnd (int d)
{
if(head==NULL)
void displayList()
{
Node *ptr=head;
while(ptr!=NULL)
{
cout<<ptr->data<<" ";
ptr=ptr->link;
}
cout<<"\n";
}
int main()
{
insertEnd(1);
insertEnd(2);
insertEnd(3);
insertEnd(4);
insertEnd(5);
displayList();
reverseList();
displayList();
return 0;
}