I have initialized the array with n elements in the constructor of Array class. Now, if I want to set the values of these elements, then by using 'set function', values are getting set after n+1 index. How do I set the values from 0 index of vector?
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
class Array{
vector<int> array;
public:
Array(int n):array(n){}
int binarySearch(int n,int i,int f)
{
int mid= (i+f)/2;
if(i!=f || array[mid]==n){
if(array[mid] == n)
return mid;
else if(array[mid] < n)
return binarySearch(n,mid+1,f);
else
return binarySearch(n,i,mid-1);
}
else
return NULL;
}
int set(int n){
array.push_back(n);
}
int size(){
return array.size();
}
void print(){
int i=0;
while(i<array.size()){
cout<<array[i]<<endl;i++;
}
}
};
int main()
{
cout<<"ENter no. of element for the array to be initialized with"<<endl;
int n,x;
cin>>n;
Array a(n);
for(int i=0;i<n;i++){
cin>>x;
a.set(x);
}
cout<<"Enter the no. to be searched"<<endl;
cin>>x;
cout<<a.binarySearch(x,0,a.size());
return 1;
}
push_back pushes to the back of the array, i.e appends after the n elements that you create in the constructor. You can use [] operator to set the elements individually. Or if you are setting them all to the same, I think there is a constructor for that.
no need to create the a Array with n objects..
simply get n numbers from the users and use the set method to push them into the vector
Related
This is the program for printing out sum of array elements. It is showing run time error. The output is coming out to be 0 instead of printing out the sum of the elements.
#include<iostream.h>
using namespace std;
void simpleArraySum()
{
int ar[100],n,i,sum=0;
for(i=0;i<n;i++)
{
sum=sum + ar[i];
}
cout<<sum;
}
int main()
{
int ar[100],n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>ar[i];
}
simpleArraySum();
return 0;
}
On this line in your main:
int ar[100], n;
You create an array of 100 elements. You later fill that array using cin
for(int i = 0 ; i < n ; i++)
{
cin >> ar[i];
}
Then you do nothing with that array. You are not calculating any sum. You let that array go, forgotten.
Then, you call a simpleArraySum function. That function is creating an entirely new, distinct array.
// v-----v------There
int ar[100],n,i,sum=0;
That array has no value assigned to it. In fact, reading from it is undefined behavior.
What you want is to receive that array in the arguments of your function:
void simpleArraySum(int* ar, int n) {
// ...
}
And call it like that in your main:
simpleArraySum(ar, 100);
You can avoid the issues of arrays and functions by not using them:
int main()
{
int quantity = 0;
std::cin >> quantity;
int sum = 0;
int value;
while (std::cin >> value)
{
sum += value;
}
std::cout << sum << "\n";
return EXIT_SUCCESS;
}
In simpleArraySum, the variable n is uninitialized. So this loop:
for(i=0;i<n;i++)
invokes undefined behavior when reading from n.
Also, you are summing a different array in the function, than the one you read in mian. It seems that you need to pass in the array from main to this function:
void simpleArraySum(int *ar, int n) {
and call it like this:
simpleArraySum(ar, n);
Finally, you don't even need a function for this, since there is an existing algorithm std::accumulate that you can use:
cout << std::accumulate(ar, ar + n, 0);
In the function, you're adding the elements of ar which is local to the function simpleArraySum() and is not of the array ar that is local to main().
So, pass the array and its length to the function and return its sum. Here is your corrected code:
#include<iostream>
using namespace std;
void simpleArraySum(int ar[], int n)
{
int i, sum = 0;
for(i=0;i<n;i++)
{
sum=sum + ar[i];
}
cout<<sum;
}
int main()
{
int ar[100],n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>ar[i];
}
simpleArraySum(ar, n);
return 0;
}
My code is:
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
struct Interval
{
int init,last;
};
bool compare(Interval a,Interval b)
{
return (a.init < b.init);
}
int main()
{
int t,i,j;
int a[1000];
cin >> t;
for( j=0;j<t;++j){
for(i=0;i<t;i++)
{
cin >> a[i];
}
}
sort(a,a+t,compare);
for( j=0;j<t;++j)
for(i=0;i<t;i++)
{
cout<<a[i]<<" ";
}
cout<<"\n";
return 0;
}
What is the solution of the below line?
sort(a,a+t,compare);
The problem is here
bool compare(Interval a,Interval b)
{
return (a.init < b.init);
}
compare compares Interval objects
But
int a[1000];
sort(a,a+t,compare);
you are trying to sort an int array.
Either sort an int array or an Interval array, but be consistent. The compare function must match the array that you are sorting.
You are attempting to sort int a[1000]; which is an int array, not an Interval array. If this is really your intention, then you do not need the predicate (compare function) for sort. You can simply use the default operator< that is provided for int. That means you code could just be:
std::sort(std::begin(a), std::begin(a) + t);
I resized my array with a function, however it seems like it does not store any array value I entered.
basically I put -1, the loop stops and then supposed to show the elements in array. but it does not show anything.
The output does not show anything after cout.
#include <iostream>
using namespace std;
void resize(int *&arr, int &size){
int tempsize=size;
size=size+10;
int *temp= new int [size];
for(int i=0; i<tempsize;i++){
temp[i]=arr[i];
}
delete [] arr;
arr=temp;
}
int main()
{
int size=0;
int capacity =10;
int *p=new int[capacity];
int check=0;
int input;
cout<<"Please enter the number in array and input -1 to end it.";
while(check!=-1)
{
cin>>input;
if(input==-1)
{check=-1;}
else{
if(size==capacity){
resize(p,capacity);
p[size]=input;
size++;
}
}
}
cout<<"Show me the numbers in array: ";
for(int i=0; i<size;i++){
cout<<p[i]<<" ";
}
cout<<endl;
delete [] p;
return 0;
}
Your if statement whether you need to resize the array is too encompassing. The {} should contain just the resize, then outside the {} is when you should store into the array.
Instead, size is 0, which != capacity, so you don't do anything.
I am using this code for the function overloading using C++ to sort a double and an integer array.
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
void sort(int arr[],int n)
{
int i,j,key;
for(j=1;j<n;j++)
{
key=arr[j];
i=j-1;
while((i>=0)&&(arr[i]>key))
{
arr[i+1]=arr[i];
i--;
}
arr[i+1]=key;
}
cout<<"Sorted integer array is \n";
for(i=0;i<n;i++)
{
cout<<arr[i]<<endl;
}
}
void sort(double arr[],int n)
{
int i,j,key;
for(j=1;j<n;j++)
{
key=arr[j];
i=j-1;
while((i>=0)&&(arr[i]>key))
{
arr[i+1]=arr[i];
i--;
}
arr[i+1]=key;
}
cout<<"Sorted double array is \n";
for(i=0;i<n;i++)
{
cout<<arr[i]<<endl;
}
}
int main()
{
int n;
cout<<"Enter the size of the array \n";
cin>>n;
cout<<"Enter the values for the integer function \n";
int a[n];
int i;
for(i=0;i<n;i++)
{
cin>>a[i];
}
double b[n];
cout<<"Enter the values for the double array \n";
for(i=0;i<n;i++)
{
cin>>b[i];
}
sort(a,n);
sort(b,n);
}
In the answer it rounds off the values of the double values except the first entry into the double array. Is there any type casting step which I am missing or any other flaw in the code?
You are irrevocably cutting off an floating part of double if you save it to int variable.
int key;
key=arr[j];
Thats why are you getting wrong results.
Declare key as double and it works.
Also VLAs (variable length arrays) are not standart C++.
Here are some other variants
std::array
std::vector
use new & delete
this is the following code
#include<iostream>
using namespace std;
int findPivot(int a[],int startIndex,int endIndex)
{
int pivot=a[endIndex];
int pivotIndex=startIndex;
for(int i=0;i<endIndex-1;i++)
{
if(a[i]<pivot)
{
int temp=a[i];
a[i]=a[pivotIndex];
a[pivotIndex]=a[i];
pivotIndex++;
}
}
int temp=pivot;//swapping pivot element into its position.
pivot=a[pivotIndex];
a[pivotIndex]=temp;
return pivotIndex;
}
void quickSort(int a[],int startingIndex,int endingIndex)
{
int number;
if(startingIndex < endingIndex)
{
int returnValueOfPivot= findPivot(a,startingIndex,endingIndex);
//cout<<returnValueOfPivot<<endl;
quickSort(a,startingIndex,returnValueOfPivot-1);//sorting for left
quickSort(a,returnValueOfPivot+1,endingIndex);//sorting for right
}
}
int main()
{
int number;
cout<<"Enter the total number of elements"<<endl;
cin>>number;
cout<<"Enter the values"<<endl;
int a[number-1];
for(int i=0;i<number;i++)
{
cin>>a[i];
}
quickSort(a,0,number-1);
for(int i=0;i<number;i++)
{
cout<<a[i]<<",";
}
return 1;
}
There are three major problems in your code :
int a[number-1];
You are allocating 1 less space for your array. Note that, array index starts from 0. So array of 5 numbers will be like
array[5] : array[0],array[1],array[2],array[3],array[4]
Swapping array values :
int temp=pivot;//swapping pivot element into its position.
pivot=a[pivotIndex];
a[pivotIndex]=temp;
Here, you swapped pivot value with a[pivotIndex] not a[endIndex]!!
So the correct swap would have been :
int temp=a[endIndex];//swapping pivot element into its position.
a[endIndex]=a[pivotIndex];
a[pivotIndex]=temp;
for(int i=0;i<endIndex-1;i++) is incorrect loop
correct loop would be :
for(int i=startIndex;i<=endIndex-1;i++)
You need to start from the start index and end till the end index. You are currently going from 0 to end - 1. [Think of the right side array loop, it won't start with 0]
Make these changes and your code will work.