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.
Related
I am trying to pass a dynamic memory allocated array and its size to a function 'sum' but it is giving error of permissive what should I do?
#include<conio.h>
#include<iostream>
using namespace std;
int sum(int n[], int *m)
{
for(int z=0;z<*m;z++)
{
cout<<"\n the output is = "<<n[z]<<"\n";
}
}
int main()
{
int *n,*m,a; //declaration is done here**strong text**
cout<<"enter the size of array = ";
m=new int;
cin>>*m;
n=new int[*m];
for(int i=0;i<*m;i++)
{
cout<<"\n enter the "<<i+1<<" array = ";
cin>>n[i];
cout<<"\n";
}
/* for(int z=0;z<*m;z++)
{
cout<<"\n the output is = "<<n[z]<<"\n";
}*/
int sum(n,&m);//here "m" is an pointer and I am trying to pass int in a function with an array
return 0;
}
Your code should, probably, look like the following (Linux Ubuntu + gcc):
#include <iostream>
using namespace std;
int sum(int n[], int m)
{
int s=0;
for(int z=0; z<m; z++)
{
cout<<"\n array["<<z<<"]= "<<n[z]<<"\n";
s+=n[z];
}
return s;
}
int main()
{
int *n,m;
cout<<"enter the size of array = ";
cin>>m;
n=new int[m];
for(int i=0; i<m; i++)
{
cout<<"\n enter array["<<i+1<<"] value = ";
cin>>n[i];
cout<<"\n";
}
int s = sum(n, m);
cout<<"s="<<s<<endl;
return 0;
}
There is no use allocating the size of the array m dynamically. It is an ordinary int variable and can be initialized as
cin>>m;
You may also write the sum prototype in the form
int sum(int * n, int m)
It is another way of passing a 1-dimensional array as a function parameter.
Speaking frankly, these questions are the very basics of the language.
You should, probably, read something like
Dynamic memory allocation/dynamic arrays
about dynamic memory allocation and dynamic arrays and
Simple cases of std::cin usage
about the simplest cases of std::cin usage in C++.
So I'm totally new to pointers, I apologize for this, I'm supposed to pass an array of pointers and get the mode of that array. After the array passes as a set of pointers, I can't manipulate the array to find the mode, everything I try results in a syntax error.
EDIT: I changed list to an array of pointers and I get a runtime error.
int main()
{
int size=0;
int *list[size];
cout<<"Please enter the size of your array: ";
cin>>size;
cout<<"\nPlease enter the numbers in your list seperated by spaces: ";
for(int i=0;i<size;i++)
{
cin>>*list[i];
}
cout<<endl;
int mode=getMode(list,size);
cout<<"\n"<<mode<<endl;
return 0;
}
int getMode (int* list[], int arraySize)
{
cout<<"The array you entered is listed below\n "<<list[0];
for(int i=0;i<arraySize;i++)
{cout<<setw(3)<<list[i];}
int *number=list[0];
int count1=0;
int count2=0;
int mode=0;
for(int j=1;j<arraySize;j++)
{
for(int i=1;i<arraySize;i++)
{
if(list[i]==number)
{
count1++; //counts the number of instances that the number occurs
}
}
if(count1>count2)
{
mode= *list[j];
count2=count1;
}
count1=0;
}
return mode;
}
When you pass an array to a function, it automatically decays to a pointer, so you don't need to use &list. And in the function, you shouldn't declare it int *list[], it should just be int list[] or int *list.
Also, in the getMode() function, you need to count the matches of list[j]. You're just counting the repetitions of number, which is list[0].
#include <iostream>
#include <iomanip>
using namespace std;
int getMode (int list[], int arraySize)
{
cout<<"The array you entered is listed below\n "<<list[0];
for(int i=0;i<arraySize;i++)
{cout<<setw(3)<<list[i];}
int count1=0;
int count2=0;
int mode=0;
for(int j=0;j<arraySize;j++)
{
for(int i=0;i<arraySize;i++)
{
if(list[i]==list[j])
{
count1++; //counts the number of instances that the number occurs
}
}
if(count1>count2)
{
mode= list[j];
count2=count1;
}
count1=0;
}
return mode;
}
int main()
{
int size;
int *list;
cout<<"Please enter the size of your array: ";
cin>>size;
list=new int[size];
cout<<"\nPlease enter the numbers in your list seperated by spaces: ";
for(int i=0;i<size;i++)
{
cin>>list[i];
}
cout<<endl;
int mode=getMode(list,size);
cout<<"\n"<<mode<<endl;
return 0;
}
DEMO
I have been asked to built a K sorted array by my professor and then sort it using minheap. A k sorted array is basically an array where each element is at most K positions away from the location it would be if the array was fully sorted in ascending order. For instance, if K=3, the element at position i=8 can be at locations 5, 6, 7, 8, 9, 10, or 11 in the fully sorted arrayI'm stuck in the first part of the problem.
Requirements:
Allow the user to enter a number of (N) elements and the number K.
Produce a K-sorted array based on the user input (more than one K-sorted array can be produced; randomly pick one). Display it.
After rigorous thinking I built the below code for K Sorting:
void kSortedArray:: displayKSorted()
{
cout<<"Please enter the no. of elements you want to KSort: ";
cin>>N;
cout<<"Please enter the value for K: ";
cin>>K;
int i=0;
while (i<N)
{
cout<<"Enter element "<<i+1<<" : ";
cin>>arr[i++];
}
insertion_sort(arr,N);
srand(time(NULL));
int output=0;
i=0;
int maxRand=0;
int minRand=0;
int arr2[N];
int a=0;
int found=0;
int found1=0;
int range=0;
int count1=0;
vector<int>:: iterator it1;
vector<int>:: iterator it2;
vector<int> arr3;
while(kSortedIndex.size()<N)
{
minRand=range-K;
maxRand=range+K;
output = minRand + (rand() % (maxRand - minRand + 1));
if (output<0)
{
output=N+output;
}
if(output>N-1)
{
output=output-N;
}
//arr2[i]=output;
for(it1=kSortedIndex.begin();it1!=kSortedIndex.end();it1++)
{
if(*it1==output)
{
found=1;
for(it2=arr3.begin();it2!=arr3.end();it2++)
{
if(*it2==output)
{
found1=1;
break;
}
}
if(found1==0)
{
arr3.push_back(output);
count1++;
}
break;
}
}
if(found==0)
{
kSortedIndex.push_back(output);
count1=0;
arr3.clear();
range++;
}
found=0;
found1=0;
if (count1>((2*K)+1))
{
range--;
count1=0;
}
if(range>(N-1))
range--;
}
vector<int>::iterator it;
int j=0;
// i=0;
for(it=kSortedIndex.begin();it!=kSortedIndex.end();it++)
{
arr2[*it]=arr[j];
j++;
//cout<<arr2[j++];
}
for(j=0;j<N;j++)
{
cout<<arr2[j];
}
}
Below is the header file:
class kSortedArray
{
public:
kSortedArray();
void displayKSorted();
protected:
private:
int N=0;
int K=0;
int* arr=new int [N];
createKSorted();
void insertion_sort(int arr[],int length);
vector <int> kSortedIndex;
};
Could someone please help me to figure out why my code crashes when I run it and runs fine when I debug it. Is it a memory leak issue? I even tried deleting arr, arr2 and clearing arr3 but that isn't working (code still cashes). Your help much appreciated.
The declaration int* arr=new int [N]; in the header is wrong. You cannot allocate memory for the array here because you do not yet know the value for N. That value will be known only after cin>>N; in the kSortedArray:: displayKSorted function. Therefore you need to do allocate memory there:
void kSortedArray:: displayKSorted()
{
cout<<"Please enter the no. of elements you want to KSort: ";
cin>>N;
arr = new int [N]; // <<< add this line
cout<<"Please enter the value for K: ";
cin>>K;
int i=0;
...
Header:
...
private:
int N=0;
int K=0;
int* arr; // =new int [N]; <<< remove the allocation
createKSorted();
void insertion_sort(int arr[],int length);
...
Disclaimer: This is non tested non error checking code, and there may be more problems.
I am trying to use bubble sort method for array with dynamically determined size. Here's the code:
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter n";
cin>>n;
int arr[n],swap;
cout<<"Enter number"<<endl;
cin>>arr[n];
for(int i=0;i<n-1;i++)
for(int j=0;i<n-i-1;j++)
if(arr[j]>arr[j+1])
{
swap=arr[j];
arr[j]=arr[j+1];
arr[j+1]=swap;
}
for(int k=0;k<n;k++)
cout<<"arr["<<k<<"]="<<arr[k]<<endl;
return 0;
}
When I define the elements of the array in this way the program works:
const n=5;
int arr[n]={1,2,3,4,5)
But I need to enter the size of the array (n) and its elements from the keyboard. But when I run my code the program crashes after I enter the first number. Is there a way to fix it?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n, swap, temp;
vector<int> arr;
cout<<"Enter n";
cin>>n;
// Loop and accept the n values.
// You may need to take care of the new line.
for(int i = 0; i < n; ++i)
{
cout << "Enter a number : ";
cin >> temp;
arr.push_back(temp);
}
for(int i=0;i<n-1;i++)
for(int j=0;j<n-i-1;j++)
if(arr[j]>arr[j+1])
{
swap=arr[j];
arr[j]=arr[j+1];
arr[j+1]=swap;
}
for(int k=0;k<n;k++)
cout<<"arr["<<k<<"]="<<arr[k]<<endl;
return 0;
}
Notice how a loop is used to extract n values from user. Also using a std::vector relieves you from writing code for a runtime sized array with new and delete.
Also, you inner loop was checking i<n-i-1 and incrementing j which should be j<n-i-1 instead, otherwise j will increment indefinitely till INT_MAX.
The key word is dynamic allocation. In C, the function is malloc. In Cpp, it can be new and delete. Although a vector can work well, it is just a kind of method by STL. Notice that my code may have safe problem.
#include <iostream>
using namespace std;
int main()
{
int n,temp;
cout<<"Enter n:";
cin>>n;
//dynamic allocation
int *arr=new int[n];
cout<<"Enter number."<<endl;
for(int i=0;i<n;i++){
cin>>arr[i];
}
//bubble sort
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
if(arr[i]>arr[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
//output the array
for(int i=0;i<n;i++){
cout<<"arr["<<i<<"]="<<arr[i]<<endl;
}
delete [] arr;
return 0;
}
You can't take an integer array like that, you need to run a loop. You can take a string like that. There are lot of errors in the bubble sort logic as well, try the below code snippet. It should work fine. You need to dynamically allocate the array for arr
int n,r,swap,i,*arr;
cout<<"Enter n\n";
cin>>n;
arr = (int *)malloc((n)*sizeof(int));
cout<<"Enter numbers\n"<<n<<endl;
for(i=0;i<n;i++)
{
cin>>arr[i];
}
for(i=0;i<n;i++)
{
for(int j=0;j<n-1;j++)//You're checking for i. you need to check for j
{
if(arr[j+1]<arr[j])
{
swap=arr[j];
arr[j]=arr[j+1];
arr[j+1]=swap;
}
}
}
//now print your arr
Includes : #include<stdlib.h>
As mentioned in my comment, you CANNOT dynamically declare the size of arrays in C++ (use std::vector if you are willing to achieve that).
You can then do this:
....
cin >> n;
vector<int> arr(n); // reserves space for `n` integers in the memory
....
I wanted to pass an array pointer to a function and show content of that array from that function.I did this in below way but when i run the code the whole program is crashing.Please help me or give me idea how to solve this problem...
int main()
{
int size,i;
cout<<"Please enter the size of the array";
cin>>size;
int *array_=new int [size];
cout<<"Please enter all elements of the array";
for(i=0;i<size;i++){
cin>>array_[i];
}
insertion(&array_,size);
return 0;
}
void insertion(int *array_[],int size){
int i;
for(i=0;i<size;i++){
cout<<*array_[i];
}
}
void insertion(int *array_,int size){
int i;
for(i=0;i<size;i++){
cout<<array_[i]<<" ";
}
}