NOTE: The code snippet given may be incomplete or have compile time, runtime or logical errors.
How to attempt Debugging Exercise?
Copy the code to your code editor (e.g. VS Code).
Add relevant header files like “#include <iostream>” etc.
Run the code.
You will notice the expected output is not printing at the console.
Apply your smart coder mind to Debug the code.
Warning: Only see the solution after you have tried enough.
Debug the code. Sum of array.
#include<iostream>
using namespace std;
int main(){
int n, sum;
cin >> n;
int input[n];
for(int i=0;i<n;i++){
cin >> input[0];
}
for(int i=0; i<n; i++){
sum = sum + input[i];
}
cout << sum << endl;
return 0;
}
void linearSearch(int arr[], int n ,int val){
for(int i=0;i<n;i++){
if(arr[i]==val){
return;
}
}
return;
}
void populate(int arr[],int n){
int j=0;
for(int i=0; i < n; i = i + 2){
arr[n-j] = i+2;
arr[j] = i+1;
j++;
}
}
void swapAlternate(int arr[],int size){
for(int i=0;i<size-1;i=i+2){
int temp = arr[i];
arr[i+1] = arr[i];
arr[i] = temp;
}
}
int pairSumToX(int input[], int size, int x){
int pairs = 0;
for(int i=0; i<size; i++){
for(int j=i; j<size; j++){
if(input[i] + input[j] == x) pairs++;
}
}
return pairs;
}
int tripletSumToX(int input[], int size, int x){
int triplets = 0;
for(int i=0; i<size; i++){
for(int j=i; j<size; j++){
for(int k=j; k<size; k++){
if(input[i] + input[j] + input[k] == x) triplets++;
}
}
}
return triplets;
}
void sort0sand1s(int input[], int size){
int nextZero = 0;
for(int i=0; i<size; i++){
if(input[nextZero] == 0){
int temp = input[nextZero];
input[nextZero] = input[i];
input[i] = temp;
}
}
}