Skip to content

Commit 150432d

Browse files
committed
added arrays folder and programs
1 parent d9f822e commit 150432d

File tree

5 files changed

+196
-0
lines changed

5 files changed

+196
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ This repo includes:
4545
- [Functions](./milestone2/functions/)
4646
- [Fahrenheit to Celsius Table](./milestone2/functions/FahrenheittoCelsiusTable.cpp)
4747
- [Fibonacci Number](./milestone2/functions/FibonacciNumber.cpp)
48+
- [Arrays](./milestone2/arrays/)
49+
- [Array Sum](./milestone2/arrays/arraysum.cpp)
50+
- [Linear Search](./milestone2/arrays/linearsearch.cpp)
51+
- [Arrange Numbers in Array](./milestone2/arrays/ArrangeNumbersinArray.cpp)
52+
- [Swap Alternate](./milestone2/arrays/SwapAlternate.cpp)
4853

4954
# Coding Ninjas
5055
## Rate my Repo ⭐!!!
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// You have been given an empty array(ARR) and its size N. The only input taken from the user will be N and you need not worry about the array.
2+
// Your task is to populate the array using the integer values in the range 1 to N(both inclusive) in the order - 1,3,5,.......,6,4,2.
3+
// Note: You need not print the array. You only need to populate it.
4+
5+
// Input Format :
6+
// The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
7+
// The first and the only line of each test case or query contains an integer 'N'.
8+
9+
// Output Format :
10+
// For each test case, print the elements of the array/list separated by a single space.
11+
// Output for every test case will be printed in a separate line.
12+
13+
//My Code
14+
void arrange(int *arr, int n)
15+
{
16+
int i,j=0;
17+
for(i = 1; i <= n; i++)
18+
{
19+
if( i % 2 != 0)
20+
{
21+
arr[j] = i;
22+
j++;
23+
}
24+
}
25+
26+
for(i = n ; i > 0;i--)
27+
{
28+
if( i%2 == 0)
29+
{
30+
arr[j] = i;
31+
j++ ;
32+
}
33+
}
34+
}
35+
36+
// Main Code
37+
#include <iostream>
38+
using namespace std;
39+
40+
#include "solution.h"
41+
42+
int main()
43+
{
44+
int t;
45+
cin >> t;
46+
while (t--)
47+
{
48+
int n;
49+
cin >> n;
50+
51+
int *arr = new int[n];
52+
arrange(arr, n);
53+
for (int i = 0; i < n; i++)
54+
{
55+
cout << arr[i] << " ";
56+
}
57+
cout << endl;
58+
delete [] arr;
59+
}
60+
}

milestone2/arrays/SwapAlternate.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// You have been given an array/list(ARR) of size N. You need to swap every pair of alternate elements in the array/list.
2+
// You don't need to print or return anything, just change in the input array itself.
3+
4+
// Input Format :
5+
// The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
6+
// First line of each test case or query contains an integer 'N' representing the size of the array/list.
7+
// Second line contains 'N' single space separated integers representing the elements in the array/list.
8+
9+
// Output Format :
10+
// For each test case, print the elements of the resulting array in a single row separated by a single space.
11+
// Output for every test case will be printed in a separate line.
12+
13+
// My Code
14+
void swapAlternate(int *arr, int size)
15+
{
16+
int temp;
17+
int i = 0, j = i+1;
18+
19+
while(j < size){
20+
temp = arr[i];
21+
arr[i] = arr[j];
22+
arr[j] = temp;
23+
i=i+2;
24+
j=i+1;
25+
}
26+
}
27+
28+
// Main Code
29+
#include <iostream>
30+
using namespace std;
31+
32+
#include "solution.h"
33+
34+
int main()
35+
{
36+
int t;
37+
cin >> t;
38+
while (t--)
39+
{
40+
int size;
41+
cin >> size;
42+
int *arr = new int[size];
43+
for (int i = 0; i < size; i++)
44+
{
45+
cin >> arr[i];
46+
}
47+
swapAlternate(arr, size);
48+
for (int i = 0; i < size; i++)
49+
{
50+
cout << arr[i] << " ";
51+
}
52+
cout << endl;
53+
delete [] arr;
54+
}
55+
}

milestone2/arrays/arraysum.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Given an array of length N, you need to find and print the sum of all elements of the array.
2+
3+
#include<iostream>
4+
using namespace std;
5+
6+
int main(){
7+
int n;
8+
cin>>n;
9+
10+
int arr[n];
11+
for(int i=0; i<n;i++){
12+
cin>>arr[i];
13+
}
14+
15+
int sum = 0;
16+
for(int i=0; i<n; i++){
17+
sum = sum+arr[i];
18+
}
19+
20+
cout<<sum<<endl;
21+
}

milestone2/arrays/linearsearch.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// You have been given a random integer array/list(ARR) of size N, and an integer X. You need to search for the integer X in the given array/list using 'Linear Search'.
2+
// You have been required to return the index at which X is present in the array/list.
3+
// If X has multiple occurrences in the array/list, then you need to return the index at which the first occurrence of X would be encountered.
4+
// In case X is not present in the array/list, then return -1.
5+
// 'Linear search' is a method for finding an element within an array/list. It sequentially checks each element of the array/list until a match is found or the whole array/list has been searched.
6+
7+
// Input format :
8+
// The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
9+
// First line of each test case or query contains an integer 'N' representing the size of the array/list.
10+
// Second line contains 'N' single space separated integers representing the elements in the array/list.
11+
// Third line contains the value of X(integer to be searched in the given array/list)
12+
13+
// Output format :
14+
// For each test case, print the index at which X is present or -1, otherwise.
15+
// Output for every test case will be printed in a separate line.
16+
17+
18+
// My Code
19+
int linearSearch(int *arr, int n, int x)
20+
{
21+
for(int i=0; i<n; i++){
22+
if (arr[i] == x){
23+
return i;
24+
}
25+
}
26+
return -1;
27+
}
28+
29+
// Main Code
30+
#include <iostream>
31+
using namespace std;
32+
33+
#include "solution.h"
34+
35+
int main()
36+
{
37+
int t;
38+
cin >> t;
39+
while (t--)
40+
{
41+
int n;
42+
cin >> n;
43+
int *arr = new int[n];
44+
for (int i = 0; i < n; ++i)
45+
{
46+
cin >> arr[i];
47+
}
48+
int val;
49+
cin >> val;
50+
cout << linearSearch(arr, n, val) << endl;
51+
}
52+
return 0;
53+
}
54+
55+

0 commit comments

Comments
 (0)