Skip to content

Commit b6f144e

Browse files
committed
miscallenous: 3
1 parent 3a176e3 commit b6f144e

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

test/Exception1.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <iostream>
2+
#include <cmath>
3+
#include <limits>
4+
#include <vector>
5+
using namespace std;
6+
7+
class Extension1
8+
{
9+
public:
10+
int getSum(const vector<int> &arr, const vector<int> &choice)
11+
{
12+
int i, total = 0;
13+
for (i = 0; i < choice.size(); i++)
14+
{
15+
total += arr[choice[i]];
16+
}
17+
18+
return total;
19+
}
20+
21+
double getSqrtSum(const vector<int> &arr, const vector<int> &choice)
22+
{
23+
int i;
24+
double total = 0.0;
25+
for (i = 0; i < choice.size(); i++)
26+
{
27+
total += sqrt(arr[choice[i]]);
28+
}
29+
30+
return total;
31+
}
32+
33+
void display(const vector<int> &arr)
34+
{
35+
int i;
36+
for (i = 0; i < arr.size(); i++)
37+
{
38+
cout << arr[i] << " ";
39+
}
40+
cout << endl;
41+
}
42+
};
43+
44+
int main()
45+
{
46+
Extension1 ob;
47+
int n, ch, i;
48+
cout << "Enter no. of inputs: ";
49+
cin >> n;
50+
51+
vector<int> arr(n);
52+
vector<int> choice(5);
53+
54+
cout << "Enter the elements:" << endl;
55+
for (i = 0; i < n;)
56+
{
57+
if (!(cin >> ch))
58+
{
59+
cout << "Invalid input. Enter again" << endl;
60+
cin.clear();
61+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
62+
continue;
63+
}
64+
arr[i++] = ch;
65+
}
66+
67+
cout << "Choose 5 indices:" << endl;
68+
for (i = 0; i < 5;)
69+
{
70+
if (!(cin >> ch) || ch < 0 || ch >= n)
71+
{
72+
cout << "Invalid input. Enter again" << endl;
73+
cin.clear();
74+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
75+
continue;
76+
}
77+
choice[i++] = ch;
78+
}
79+
80+
cout << "Inputs: ";
81+
ob.display(arr);
82+
cout << "Choices: ";
83+
ob.display(choice);
84+
85+
int sum;
86+
double res;
87+
sum = ob.getSum(arr, choice);
88+
res = ob.getSqrtSum(arr, choice);
89+
90+
cout << "Sum = " << sum << endl;
91+
cout << "Sqrt Sum = " << res << endl;
92+
93+
return 0;
94+
}

test/Exception1.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import java.util.*;
2+
3+
public class Exception1 {
4+
int getSum(int arr[], int choice[]) {
5+
int i, total = 0;
6+
for (i = 0; i < choice.length; i++) {
7+
total += arr[choice[i]];
8+
}
9+
10+
return total;
11+
}
12+
13+
double getSqrtSum(int arr[], int choice[]) {
14+
int i;
15+
double total = 0.0;
16+
for (i = 0; i < choice.length; i++) {
17+
total += Math.sqrt(arr[choice[i]]);
18+
}
19+
20+
return total;
21+
}
22+
23+
void display(int arr[]) {
24+
int i;
25+
for (i = 0; i < arr.length; i++) {
26+
System.out.print(arr[i] + " ");
27+
}
28+
System.out.println();
29+
}
30+
31+
public static void main(String[] args) {
32+
Scanner sc = new Scanner(System.in);
33+
Exception1 ob = new Exception1();
34+
int i, n, ch;
35+
36+
System.out.println("Enter size");
37+
n = sc.nextInt();
38+
int arr[] = new int[n];
39+
int choice[] = new int[5];
40+
41+
System.out.println("Enter elements:");
42+
for (i = 0; i < n;) {
43+
try {
44+
ch = sc.nextInt();
45+
} catch (InputMismatchException e) {
46+
System.out.println("Invalid Input. Enter again");
47+
sc.next();
48+
continue;
49+
}
50+
arr[i++] = ch;
51+
}
52+
53+
System.out.println("Enter 5 indices:");
54+
for (i = 0; i < 5;) {
55+
try {
56+
ch = sc.nextInt();
57+
if (ch < 0 || ch >= n) {
58+
throw new Exception("Invalid Index");
59+
}
60+
} catch (InputMismatchException e) {
61+
System.out.println("Invalid Input. Enter again");
62+
sc.next();
63+
continue;
64+
} catch (Exception e) {
65+
System.out.println(e.getMessage());
66+
// sc.next();
67+
continue;
68+
}
69+
70+
choice[i++] = ch;
71+
}
72+
73+
System.out.print("Arr = ");
74+
ob.display(arr);
75+
System.out.print("Choice = ");
76+
ob.display(choice);
77+
78+
int res1;
79+
double res2;
80+
res1 = ob.getSum(arr, choice);
81+
res2 = ob.getSqrtSum(arr, choice);
82+
83+
System.out.println("Sum = " + res1);
84+
System.out.println("Sqrt Sum = " + res2);
85+
86+
sc.close();
87+
}
88+
}

0 commit comments

Comments
 (0)