0% found this document useful (0 votes)
33 views

Assignment - 3

begginer friendly codes
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Assignment - 3

begginer friendly codes
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Sudhanshu raj -- 2023ugcs094

Assignment – 3
Q1---->
ANS
#include<bits/stdc++.h>

using namespace std;

double median(int n,vector<int>&v){

sort(v.begin(),v.end());

cout << "Median of the given array is: ";

if(n&1){

return v[(n/2)];

return (double)(v[n/2] + v[(n-1)/2])/2;

int main(){

int n;

cout << "Enter the size of array: ";

cin >> n;

vector<int>v(n);

cout <<"Enter elements of the array: ";

for(int i=0;i<n ;i++) cin >> v[i];

cout << median(n,v);

OUTPUT
Q2 ------
ANS-
#include<bits/stdc++.h>

using namespace std;

int mode( int n,vector<int>&v){

map<int,int>mp;

for(auto c:v) mp[c]++;

int mode_value = 0;

int mode_cnt = 0;

for(auto c:mp){

if(c.second>mode_cnt){

mode_cnt = c.second;

mode_value = c.first;

int cnt_mode = 0;

for(auto c:mp){

if(c.second == mode_cnt)cnt_mode++;

if(cnt_mode>1) return -1; // assuming all elements are positive

return mode_value;

}
int main(){

int n;

cout << "Enter the size of array: ";

cin >> n;

cout << "Enter the elements of array: ";

vector<int>v(n);

for(int i=0;i<n;i++)cin >> v[i];

int m = mode(n,v);

if(m==-1){

cout << "Mode of given array does not exist.";

else cout << m << endl;

return 0;

OUTPUT

Q3--------->
ANS
#include<bits/stdc++.h>
using namespace std;

int main(){

float arr[181];

float cnt[181];

for(int i=0;i<181;i++){

arr[i] = 0;

cnt[i] = 0;

int n;

cout << "Enter the number of data: ";

cin >> n;

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

cout << "Enter latitude and temperature respectively: ";

int a;

float b;

cin >> a >> b;

arr[a+90] = arr[a+90]*cnt[a+90] + b;

cnt[a+90]++;

arr[a+90] = arr[a+90]/cnt[a+90];

for(int i=-90;i<91;i++){

if(arr[i+90] == 0) cout << i << " " << "NO DATA" << endl;

else cout << i << " " << arr[i+90] << endl;

int cnt_s = 0;

int cnt_n = 0;

float avg_s = 0;

float avg_n = 0;

for(int i=-90;i<0;i++){

if(arr[i+90]!=0)cnt_s++;

avg_s += arr[i+90];
}

for(int i=1;i<91;i++){

if(arr[i+90]!=0)cnt_n++;

avg_n += arr[i+90];

if(cnt_n == 0) cnt_n ++;

if(cnt_s == 0)cnt_s ++ ;

avg_n /= cnt_n;

avg_s /= cnt_s;

cout << "Average of southern hemisphere is: " << avg_s << endl;

cout << "Average of northern hemisphere is: " << avg_n << endl;

avg_n = 0;

avg_s = 0;

cnt_n = 0;

cnt_s = 0;

for(int i=-90;i<=0;i++){

if(arr[i+90]!=0 && arr[90-i]!=0){

avg_n += arr[i+180];

avg_s += arr[i+90];

cnt_n++;

cnt_s++;

avg_n /= cnt_n;

avg_s /= cnt_s;

if(avg_n<avg_s){

cout << "Southern hemisphere is warmer.\n";

else cout << "Northern hemisphere is warmer. \n";

}
OUTPUT

Q4------>
ANS
#include<bits/stdc++.h>

using namespace std;

int main(){

int baseaddress = 100;

//question-a

int a[100];

cout << (&a[10] - &a[0]) * 4 + baseaddress << endl;

//question-b

int b[200];

cout << (&b[100] - &b[0]) * 4 + baseaddress << endl;

//question-c

int c[10][20];

cout << (&c[0][0] - &c[0][0]) * 4 + baseaddress << endl;


//question-d

int d[10][20];

cout << (&d[2][1] - &d[0][0]) * 4 + baseaddress << endl;

//question-e

int e[10][20];

cout << (&e[5][1] - &e[0][0]) * 4 + baseaddress << endl;

//question-f

int f[10][20];

cout << (&f[1][10] - &f[0][0]) * 4 + baseaddress << endl;

//question-g

int g[10][20];

cout << (&g[2][10] - &g[0][0]) * 4 + baseaddress << endl;

//question-h

int h[10][20];

cout << (&h[5][3] - &h[0][0]) * 4 + baseaddress << endl;

//question-i

int i[10][20];

cout << (&i[9][19] -&i[0][0]) * 4 + baseaddress << endl;

return 0;

OUTPUT
Q5----->
ANS--->
#include<bits/stdc++.h>

using namespace std;

struct realtype

int left;

int right;

};

struct realtype to_realtype(long double a){

int l = a;

int r = (a-l)*(1e9);

if(r<0)r*=-1;

struct realtype t;

t.left = l;

t.right = r;

return t;

}
long double to_real(struct realtype temp){

int l = temp.left;

int r = temp.right;

long double p = ceil(log10(r));

p = pow(10,p);

long double after_decimal = (long double)r/p;

long double ans = (long double)l + after_decimal;

return ans;

struct realtype real_multiply(struct realtype temp1, struct realtype temp2){

long double a = to_real(temp1);

long double b = to_real(temp2);

long double c = a*b;

return to_realtype(c);

struct realtype real_add(struct realtype temp1, struct realtype temp2){

long double a = to_real(temp1);

long double b = to_real(temp2);

long double c = a + b;

cout << c << endl;

return to_realtype(c);

struct realtype real_sub(struct realtype temp1, struct realtype temp2){

long double a = to_real(temp1);

long double b = to_real(temp2);

long double c = a - b;

return to_realtype(c);

int main(){

//question a:
long double n;

cout << "Enter a real number: ";

cin >> n;

struct realtype temp = to_realtype(n);

cout << temp.left << "." << temp.right << endl;

//question b:

struct realtype temp2;

cout << "Enter left part of real number: ";

cin >> temp2.left;

cout << "Enter right part of real number: ";

cin >> temp2.right;

long double real = to_real(temp2);

cout << real << endl;

//question c:

struct realtype temp3, temp4, add, subtract, multiply;

cout << "Enter left part of first number: ";

cin >> temp3.left;

cout << "Enter right part of first number: ";

cin >> temp3.right;

cout << "Enter left part of second number: ";

cin >> temp4.left;

cout << "Enter right part of second number: ";

cin >> temp4.right;

multiply = real_multiply(temp3, temp4);

cout << "Multiplication: " << multiply.left << " " << multiply.right << endl;

add = real_add(temp3, temp4);

cout << "Addition: " << add.left << " " << add.right << endl;

subtract = real_sub(temp3, temp4);

cout << "Subtraction: " << subtract.left << " " << subtract.right << endl;
return 0;

OUTPUT

Q6--->
ANS--->
#include<bits/stdc++.h>

using namespace std;

int main(){

struct student

string first_name;

string last_name;

float grade_ind;

};
struct employee

string first_name;

string last_name;

float salary;

};

int n;

cout << "Enter number of records: ";

cin >> n;

struct student std[n];

struct employee emp[n];

cout << "Enter students record -> \n";

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

cout << "Enter last name of student " << i+1 << ": ";

cin >> std[i].last_name;

cout << "Enter first name of student " << i+1 << ": ";

cin >> std[i].first_name;

cout << "Enter grade point index of student " << i+1 << ": ";

cin >> std[i].grade_ind;

cout << "Enter employees record -> \n";

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

cout << "Enter last name of employee " << i+1 << ": ";

cin >> emp[i].last_name;

cout << "Enter first name of employee " << i+1 << ": ";

cin >> emp[i].first_name;

cout << "Enter salary of employee " << i+1 << ": ";

cin >> emp[i].salary;

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

for(int j=0;j<n;j++){

if(emp[i].last_name == std[j].last_name && emp[i].first_name == std[j].first_name){

if(std[j].grade_ind > (3.0)) emp[i].salary *= 1.1;

}}}

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

cout << emp[i].last_name << " " << emp[i].first_name << " " << emp[i].salary << endl;

return 0;

OUTPUT----

You might also like