Counting elements in an array greater than next element C++ - c++

Why is it giving wrong count
In this question we have to find out how many elements are greater than the next element of an array
#include
using namespace std;
int main() {
//number of testcases
int t;
cin>>t;
while(t--){
//taking number of elements in an array
int n,count;
cin>>n;
//taking array
int a[n];
count=0;
for(int i=1;i<=n;i++){
cin>>a[n];
}
//checking array
for(int j=1;j<=n-1;j++){
if(a[j]>a[j+1])
{
count++;
}
}
cout<<count<<endl;
}
return 0;
}
Input
5
1 2 3 4 1
Expected Output
1
Output through above code
2

There are several problems with your code:
Problem 1
You're going out of bounds of the array a which leads to undefined behavior, when you wrote
cin >> a[n]; //undefined behavior
Note that indexing of arrays starts from 0 instead of 1 in C++.
Problem 2
In standard C++, the size of an array must be a compile-time constant. This mean the following is incorrect in your code:
int n;
cin>>n;
int a[n]; //NOT STANDARD C++ BECAUSE n IS NOT A CONSTANT EXPRESSION
Better would be to use std::vector instead of built in array.
int n = 0;
std::cin >> n;
std::vector<int> a(n); //this creates a vector of size n
//take input from user
for(int& element: a)
{
std::cin >> element;
}
int count = 0;
for(int i = 0; i< n - 1; ++i)
{
if(a[i] > a[i+1])
{
++count;
}
}

Related

Partition of Array by Element given X

I Am Trying To Find Partition Of Array ,On Condition By Checking Variable x ,when less then x they will be on one side or else on another. but my code need some correction.
HERE am not able to find the error , i will be thankful to you if you help me.
Code is:-
#include<iostream>
using namespace std;
int partition(int arr[],int n,int x){
for(int i=0;i<n;){
if(arr[i]<x){
i++;
}
else if(arr[i]==x){
int temp=arr[i];
arr[i]=arr[n];
arr[n]=temp;
i--;
}
else if(arr[i]>x){
int temp=arr[i];
for(int j=i;j<n;j++){
arr[j]=arr[j+1];
}
arr[n]=temp;
i--;
}
}
return 0;
}
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int x;
cin>>x;
partition(arr,n,x);
for(int i=0;i<n;i++){
cout<<arr[i]<<"\t";
}
return 0;
}
Input >> array={2,10,15,1,3,15} ,x=10
Expected << {2,1,3,10,15,15}
Output I get << nothing .
The code isn't giving any output because, first, the "cin" and "cout" are in upper case which is syntactically incorrect, secondly, the variable j is in different case in loop statement and body inside the second else-if clause in the partition function, same goes for the "I" in the first for loop in the main() function. Sort this out and you should be good to go.
First in C++ the size of an array must be a compile-time constant. So for example, consider the following examples:
int n = 10;
int arr[n]; //INCORRECT
The correct way to write the above would be:
const int n = 10;
int arr[n]; //CORRECT
Similarly, in your code,
int n;
cin>>n;
int arr[n]; //INCORRECT because n is not a constant expression
Second, in your code, when you wrote:
arr[n] = temp; Undefined behavior
you're going out of bounds and so you have undefined behavior.
Solution
You can use std::stable_partition and std::vector to solve your problem as shown below:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
int n;
std::cout <<"Enter n:"<<std::endl;
std::cin >> n;
std::vector<int> arr(n); //create a vector of size n instead of an array
std::cout<<"Enter elements: "<<std::endl;
//iterate and take input from user
for(int &elem: arr){
std::cin >> elem ;
}
int x;
std::cout << "Enter x: "<<std::endl;
std::cin>>x;
//use std::partition
std::stable_partition(arr.begin(), arr.end(), [x](int i){return (i < x);});
std::cout<<"This is the partitioned vector: "<<std::endl;
for(int i=0;i<n;i++)
{
std::cout<<arr[i]<<"\t";
}
return 0;
}
Output
The output of the above program is as follows:
Enter n:
6
Enter elements:
2
10
15
1
3
15
Enter x:
10
This is the partitioned vector:
2 1 3 10 15 15
which can be seen here.

what if i swap an element with -1 index of array

// ques-move all negative ele to one side
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int arr[n];
for(int i=0;i<n;i++){
cin >> arr[i];
}
int j=-1;
int pivot = 0;
for(int i=0;i<n;i++){
if(arr[i]<pivot){
swap(arr[i],arr[j])
i++;
}
}
return 0;
}`
if we swap 1st index with -1 th index if it is negative then what will happen will a garbage value take place at arr[1]
Accessing an array outside of its bounds of 0 to n-1 is undefined behavior. The most likely outcome is a garbage value, but literally anything could happen - your program could crash, you could erase your entire disk drive, or demons could fly from your nose.

I want to print array in reverse order, I tried to code for the same as shown in the attached picture but I am not understanding why is 0 being shown?

this is the code which I tried
int main(){
int n;
cout<<"Type the number of elements\n";
cin>>n;
cout<<"Type the integars";
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<"This is the reversed array ";
for(int i=n;i>=0;i--){
cout<<arr[i]<<" ";
}
return 0;
}
this is the output pls explain why is 0 coming where did I go wrong?
The problem is that your reversed array starts at n, and arr[n] is invalid, because the array starts at 0.
for (int i = n - 1; i >= 0; i--) {
cout << arr[i] << " ";
}
Your array is of size 5, but you're accessing 6 elements instead 5. That is, you are accessing elements 5...0 (6 in total) when the array only has 4...0 (5 in total) elements.
Start the 2nd for loop with n-1 and that should solve your issue. That is, (for(int i=n-1;i>=0;i--)) instead of (for(int i=n;i>=0;i--))
Solution: To print use i = n - 1
int main(){
int n;
cout<<"Type the number of elements\n";
cin>>n;
cout<<"Type the integars";
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<"This is the reversed array ";
for(int i=n-1;i>=0;i--){
cout<<arr[i]<<" ";
}
return 0;
}
Arrays begin counting at zero through to n (n being the target size value), so you need to ignore the zero value and start printing after zero has been iterated through.
Something like:
counter = target -1
When printing the reverse of array make sure you remember the position of elements is always one less.
So you have to start with n-1. '0' is getting printed because it is the garbage value at that position it could have been any number or value.
int main(){
int n;
cout<<"Type the number of elements\n";
cin>>n;
cout<<"Type the integars";
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<"This is the reversed array ";
// When printing the reverse of array make sure you remember the position of elements is always one less.
// So you have to start with n-1. '0' is getting printed because it is the garbage value at that position it could have been any number or value.
for(int i=n-1;i>=0;i--){
cout<<arr[i]<<" ";
}
return 0;
}

The output of this question is correct but i am getting a segmentation error

I am trying to solve the program of array rotation. I am getting segmentation error in the code. Can someone please tell where is the problem in this code?
this is the question
Given an unsorted array arr[] of size N, rotate it by D elements (clockwise).
The first line of the input contains T denoting the number of testcases. First line of each test case contains two space separated elements, N denoting the size of the array and an integer D denoting the number size of the rotation. Subsequent line will be the N space separated array elements.
and i have solved it with the following code.
#include <iostream>
using namespace std;
int* rotate(int ar[],int n, int m)
{static int temp[100];
for(int i =0;i<m;i++)
{
temp[i]=ar[i];
}
for(int j =m;j<n;j++)
{
ar[j-m]=ar[j];
}
int x=0;
for(int k =n-m;k<n;k++)
{
ar[k]=temp[x];
x++;
}
return ar;
}
int main() {
//code
int t, n , m;
cin>>t;
while(t>0)
{
cin>>n>>m;
int arr[n];
int * ptr;
for(int i = 0 ;i<n;i++)
{
cin>>arr[i];
}
ptr=rotate(arr,n,m);
for(int j=0;j<n;j++)
cout<<ptr[j]<<" ";
cout<<endl;
t--;
}
return 0;
}
If m > n then it crashes in the first for() loop in rotate as you index past the end of arr.
If m < 0 it crashes in the 2nd loop as it index before arr.
There are probably more cases.

Why is IDEone showing runtime error even after getting the output?

What is wrong with my program ?
It works fine on my PC but in IDEone it gives the correct output but shows runtime error. Please help.
#include<bits/stdc++.h>
using namespace std;
struct student
{
int vote;
};
int main()
{
int t;
cin>>t;
while(t--)
{
int count=0;
int n;
cin>>n;
vector <int> a(n);
student s[n];
int k;
cin>>k;
for(int i=1;i<=n;i++)
{
s[i].vote=0;
}
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
int temp=0;
for(int i=1;i<=n;i++)
{
if(a[i] != i)
{
temp=a[i];
s[temp].vote++;
}
}
for(int i=1;i<=n;i++)
{
if(s[i].vote==k)
{
count++;
}
}
printf("%d\n",count);
}
return 0;
}
This is the error shown in IDEone :-
Error in `./prog': free(): invalid next size (fast): 0x085cca10
student s[n];
This declares an array called s. It contains n values. The values are s[0] through s[n-1] (you can count them all on your fingers, if you'd like, using a small number of n, such as 5).
for(int i=1;i<=n;i++)
{
s[i].vote=0;
}
This attempts to initialize values s[1] through s[n]. The only problem is that s[n] doesn't exist. The last value in the array is s[n-1]. This code will corrupt memory on the stack, resulting in undefined behavior.
The same bug also occurs with the a array.
The index values for both vector and array are zero based and goes up to n - 1. So
for(int i = 0; i != n; ++i)
would be better.
Now you write one element too far, which free finds out later when the data after the memory block is invalid.