In an array of N elements (N is given), find the smallest element from the first zero element to the end of the array. If there are no zero elements in the array, display a message about it.
Can someone fix this for me please?
#include<iostream>
using namespace std;
int main() {
int i, n;
cout << "N= "; cin >> n;
if (n > 0) {
int *a = new int[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
for (i = 0; i < n; i++) {
if (a[0] > a[i])
a[0] = a[i];
}
cout << "\nMin:" << a[0];
delete[] a;
}
return 0;
}
I couldn't find the problem with your code so I am showing how I approached finding a minimum element from a given array:
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int minNo=INT16_MAX; // to assign max. possible value to minNo
for(int i=0;i<n;i++){
minNo=min(arr[i],minNo);
}
cout<<minNo<<endl;
return 0;
}
You can simply check it in the same loop in which you looking for smallest element.
#include <iostream>
#include <climits>
using namespace std;
int main() {
int i, n;
bool isZeroElement = false;
cout << "N= "; cin >> n;
if (n > 0) {
int *a = new int[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
int minElement = INT_MAX;
for (i = 0; i < n; i++) {
if (!isZeroElement && a[i] == 0)
{
isZeroElement = true;
continue;
}
if (isZeroElement && minElement > a[i])
minElement = a[i];
}
if (!isZeroElement)
cout << "There is no zero element in the array\n";
else
cout << "\nMin:" << minElement;
delete[] a;
}
return 0;
}
Related
As you see in my code, I have to return an array through return function. What necessary thing do I have to change in my code to disable the warning of return type? When I run this code, it gives me a warning. I have to remove this warning:
warning: no return statement in function returning non-void [-Wreturn-type]
#include <bits/stdc++.h>
using namespace std;
int sort(int a[], int n)
{
int zeros = 0;
for (int i = 0; i < n; i++)
{
if (a[i] == 0)
{
zeros++;
}
}
int k = 0;
while (zeros--)
{
a[k++] = 0;
}
while (k < n)
{
a[k++] = 1;
}
}
int main()
{
int n;
cout << "Enter the n value : ";
cin >> n;
int a[n];
cout << "Enter the array elements : ";
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
sort(a, n);
for (int i = 0; i < n; i++)
{
cout << a[i];
}
return 0;
}
int sort(int a[], int n)
Your sort function is declared to return an int, but you didn't return anything. If you don't want to return anything, declare it using void.
Also, VLA is not valid C++. You should use std::vector instead.
So, your code should look like this.
#include <iostream>
#include <vector>
void sort(std::vector<int> &a) // no need for n
{
int zeros = 0;
for (int i = 0; i < a.size(); i++) // use member function size() instead for n
{
if (a[i] == 0)
{
zeros++;
}
}
int k = 0;
while (zeros--)
{
a[k++] = 0;
}
while (k < n)
{
a[k++] = 1;
}
}
int main()
{
int n;
std::cout << "Enter the n value : ";
std::cin >> n;
std::vector<int> a(n);
std::cout << "Enter the array elements : ";
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
sort(a);
for (cons auto &i : a) // range-base for loop, recommend
{
std::cout << a[i];
}
return 0;
}
Note: using namespace std; and #include <bits/stdc++.h> are both bad practice, so avoid using it.
My program is to find the smallest positive number missing from an array. With the following input I expect an output of 2.
6
0
-9
1
3
-4
5
My problem is that it does not give any output. Can anyone explain this please?
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int array[n];
for (int i = 0; i < n; i++)
{
cin >> array[n];
}
int const N = 1e4+2;
bool indexarray[N];
for (int i = 0; i < N; i++)
{
indexarray[i] = false;
}
for (int i = 0; i < n; i++)
{
if (array[i] > 0)
{
indexarray[array[i]] = true;
}
}
int ans = -1;
for (int i = 1; i < N; i++)
{
if (indexarray[i] == false)
{
ans = i;
}
}
cout << ans << endl;
return 0;
}
I think because int array[n]; makes an array called array with n elements in it, with the first one starting at array[0].
cin >> array[n]; needs to modify array[n], but because the first element is array[0], the last element is array[n-1], and array[n] does not exist. Your code gave an error and exited.
Try changing
for (int i = 0; i < n; i++)
{
cin >> array[n];
}
to
for (int i = 0; i < n; i++)
{
cin >> array[i];
}
Also, I think variable length arrays are non-standard, so maybe try changing that. Replace it with std::vector<int> array(n) should work.
I've been stuck on this problem for a pretty Long while. I always get "Time limit exceeded" when I submit the code.
My solution is to input the items of the array then determine the largest number in the array and diplay it along with the elements following it and so on.
How can I make my algorithm more efficient?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int T, n;
cin >> T;
while (T--) {
//inputting p[n]
scanf_s("%d", &n);
int* p = new int[n];
for (int i = 0; i < n; i++) {
scanf_s("%d", &p[i]);
}
while (n != 0) {
//*itr = largest element in the array
auto itr = find(p, p + n, *max_element(p, p + n));
int index = distance(p, itr);
for (int i = index; i < n; i++) {
printf("%d\n", p[i]);
}
//deleting element by decreasing n:
n = index;
}
delete[] p;
}
return 0;
}
You solution is O(n^2), too slow.
A O(n) solution is obtained by iteratively calculating the position of the max element until a given index i.
#include <iostream>
#include <vector>
#include <algorithm>
//using namespace std;
int main() {
int T;
std::cin >> T;
while (T--) {
//inputting p[n]
int n;
std::cin >> n;
std::vector<int> p(n);
for (int i = 0; i < n; i++) {
std::cin >> p[i];
}
std::vector<int> max_before(n);
max_before[0] = 0;
for (int i = 1; i < n; ++i) {
if (p[i] > p[max_before[i-1]]) {
max_before[i] = i;
} else {
max_before[i] = max_before[i-1];
}
}
while (n != 0) {
int index = max_before[n-1];
for (int i = index; i < n; i++) {
std::cout << p[i] << " ";
}
//deleting element by decreasing n:
n = index;
}
std::cout << '\n';
}
return 0;
}
Sigabrt runtime error occurs of a fatal error, because of an assert statement not returning true? Or use of excessive memory, I'm not able to figure out what I'm doing wrong here, help me out?
( problem 1343 C on codeforces) link
so here's the code.
#include <iostream>
#include <stdlib.h>
#include<vector>
using namespace std;
int check(int i,vector<int> a) {
if (a[i] > 0) {
return 1;
}
else return 0;
}
int main() {
int t;
cin >> t;
while (t--)
{
long int n;
cin >> n;
vector<int> a(n), b;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int i = 0;
while (i < n)
{
int max = a[i];
int s = check(i,a);
i++;
while (i<n && check(i,a)== s) {
if (a[i] > max)max = a[i];
i++;
}
b.push_back(max);
}
int s = 0;
for (int k = 0; k< b.size(); k++) {
s += b[i];
}
cout << s << endl;
}
}
I have debugged your code and also the modified code has been accepted for the above question.
Mistakes you made:
1. In the below loop, value at i'th index of vector<int> b is being added to long int s. Instead, b[k] should be added to long int s because the variable being used in the loop is k not i.
for (int k = 0; k< b.size(); k++) {
s += b[i];
}
2. In the question, range of variable n is given as (1 ≤ n ≤ 2.10^5). So, it is safe to use int n instead of long int n. Also, when I submitted my code on codeforces it gave me signed integer overflow error when I used long int n.
3. You need to use long long s instead of long int s because the value of each element of array A lies between (−10^9 ≤ a[i] ≤ 10^9 , ai ≠ 0) and when we add the elements it can easily surpass int and long int ranges.
4. Although, the answer got accepted when I used vector<int> a in the function
int check(int i,vector<int> a) {
if (a[i] > 0) {
return 1;
}
else return 0;
}
But as the user Scheff has said and is correct that it comes with a penalty in space and time, you should use call by reference i.e. vector<int> &a.
Modified Code:
#include <iostream>
#include <stdlib.h>
#include<vector>
using namespace std;
int check(int i, vector<int> &a) {
if (a[i] > 0) {
return 1;
}
else return 0;
}
int main() {
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<int> a(n), b;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int i = 0;
while (i < n)
{
int max = a[i];
int s = check(i,a);
i++;
while ((i<n) && (check(i,a)== s)) {
if (a[i] > max)
max = a[i];
i++;
}
b.push_back(max);
}
long long s = 0;
for (int k = 0; k< b.size(); k++) {
s += b[k];
}
cout << s << endl;
}
}
Screenshot of Accepted Answer:
I'm writing a program that has a user input integers into an array, calls a function that removes duplicates from that array, and then prints out the modified array. When I run it, it lets me input values into the array, but then gives me a "Segmentation fault" error message when I'm done inputing values. What am I doing wrong?
Here is my code:
#include <iostream>
using namespace std;
void rmDup(int array[], int& size)
{
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (array[i] == array[j])
{
array[i - 1 ] = array[i];
size--;
}
}
}
}
int main()
{
const int CAPACITY = 100;
int values[CAPACITY], currentSize = 0, input;
cout << "Please enter a series of up to 100 integers. Press 'q' to quit. ";
while (cin >> input)
{
if (currentSize < CAPACITY)
{
values[currentSize] = input;
currentSize++;
}
}
rmDup(values, currentSize);
for (int k = 0; k < currentSize; k++)
{
cout << values[k];
}
return 0;
}
Thank you.
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (array[i] == array[j])
{
array[i - 1 ] = array[i]; /* WRONG! array[-1] = something */
size--;
}
}
}
If array[0] and array[1] are equal, array[0-1] = array[0], meaning that array[-1] = array[0]. You are not supposed to access array[-1].
I wouldn't make it even possible to create duplicates:
int main()
{
const int CAPACITY = 100;
cout << "Please enter a series of up to 100 integers. Press 'q' to quit. ";
std::set<int> myInts;
int input;
while (std::cin >> input && input != 'q' && myInts.size() <= CAPACITY) //note: 113 stops the loop, too!
myInts.insert(input);
std::cout << "Count: " << myInts.size();
}
And do yourself a favour and don't use raw arrays. Check out the STL.
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
vector<int> vec = {1,1,2,3,3,4,4,5,6,6};
auto it = vec.begin();
while(it != vec.end())
{
it = adjacent_find(vec.begin(),vec.end());
if(it != vec.end())
vec.erase(it);
continue;
}
for_each(vec.begin(),vec.end(),[](const int elem){cout << elem;});
return 0;
}
This code compiles with C++11.
#include<iostream>
#include<stdio.h>
using namespace std;
int arr[10];
int n;
void RemoveDuplicates(int arr[]);
void Print(int arr[]);
int main()
{
cout<<"enter size of an array"<<endl;
cin>>n;
cout<<"enter array elements:-"<<endl;
for(int i=0;i<n ;i++)
{
cin>>arr[i];
}
RemoveDuplicates(arr);
Print(arr);
}
void RemoveDuplicates(int arr[])
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;)
{
if(arr[i]==arr[j])
{
for(int k=j;k<n;k++)
{
arr[k]=arr[k+1];
}
n--;
}
else
j++;
}
}
}
void Print(int arr[])
{
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}