Skip to content

Commit bf357dd

Browse files
commit
1 parent dbe3b29 commit bf357dd

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

Arrays (Sum of array).cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//{ Driver Code Starts
2+
#include <iostream>
3+
using namespace std;
4+
5+
// } Driver Code Ends
6+
class Solution
7+
{
8+
public:
9+
int getSum(int a[], int n)
10+
{
11+
int sum = 0;
12+
for (int i = 0; i < n; i++)
13+
{
14+
sum += a[i];
15+
}
16+
return sum;
17+
}
18+
};
19+
20+
//{ Driver Code Starts.
21+
int main()
22+
{
23+
int t;
24+
cin >> t;
25+
while (t--)
26+
{
27+
int n;
28+
cin >> n;
29+
int a[n];
30+
for (int i = 0; i < n; i++)
31+
cin >> a[i];
32+
Solution ob;
33+
cout << ob.getSum(a, n) << endl;
34+
}
35+
36+
return 0;
37+
}
38+
// } Driver Code Ends

Print Linked List elements.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//{ Driver Code Starts
2+
#include <iostream>
3+
#include <cstdio>
4+
#include <cstdlib>
5+
6+
using namespace std;
7+
8+
struct Node
9+
{
10+
int data;
11+
struct Node *next;
12+
} *start;
13+
14+
void insert();
15+
16+
// } Driver Code Ends
17+
/*
18+
Print elements of a linked list on console
19+
head pointer input could be NULL as well for empty list
20+
Node is defined as
21+
struct Node
22+
{
23+
int data;
24+
Node *next;
25+
}
26+
*/
27+
28+
class Solution
29+
{
30+
public:
31+
void display(Node *head)
32+
{
33+
Node *temp = head;
34+
while (temp != NULL)
35+
{
36+
cout << temp->data << " ";
37+
temp = temp->next;
38+
}
39+
}
40+
};
41+
42+
//{ Driver Code Starts.
43+
44+
int main()
45+
{
46+
int t;
47+
cin >> t;
48+
while (t--)
49+
{
50+
start = NULL;
51+
insert();
52+
Solution ob;
53+
ob.display(start);
54+
cout << endl;
55+
}
56+
return 0;
57+
}
58+
59+
void insert()
60+
{
61+
int n, value;
62+
cin >> n;
63+
struct Node *temp;
64+
for (int i = 0; i < n; i++)
65+
{
66+
cin >> value;
67+
if (i == 0)
68+
{
69+
start = (struct Node *)malloc(sizeof(struct Node));
70+
start->data = value;
71+
start->next = NULL;
72+
temp = start;
73+
continue;
74+
}
75+
else
76+
{
77+
temp->next = (struct Node *)malloc(sizeof(struct Node));
78+
temp = temp->next;
79+
temp->data = value;
80+
temp->next = NULL;
81+
}
82+
}
83+
}
84+
85+
// } Driver Code Ends

Sum of Array Elements.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//{ Driver Code Starts
2+
// Initial Template for C++
3+
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
// } Driver Code Ends
8+
// User function Template for C++
9+
10+
// Complete this function
11+
12+
int sumElement(int arr[], int n)
13+
{
14+
int sum = 0;
15+
for (int i = 0; i < n; i++)
16+
{
17+
sum += arr[i];
18+
}
19+
return sum;
20+
}
21+
22+
//{ Driver Code Starts.
23+
24+
int main()
25+
{
26+
int t;
27+
cin >> t;
28+
while (t--)
29+
{
30+
int n;
31+
cin >> n;
32+
int arr[n];
33+
for (int i = 0; i < n; i++)
34+
cin >> arr[i];
35+
36+
cout << sumElement(arr, n) << endl;
37+
}
38+
return 0;
39+
}
40+
// } Driver Code Ends

0 commit comments

Comments
 (0)