#include <iostream>
using namespace std;
char problem5(char alc[], char a, int *n);
int main() {
char aloc1[]={ 'g','g','c','g','a','g','g','g','t','g'};
int size=sizeof(aloc1)/sizeof(aloc1[0]);
cout << aloc1 << endl;
int nalc = problem5(aloc1, 'g' ,&size);
cout << nalc << endl;
return 0;
}
char problem5(char alc[], char c, int *n){
int a = 0;
for(int i = 0; i < n; i++){
if(alc[0]!=c){
a++;
}
}
int nalc[a];
int b=0;
for(int j = 0; j < n; j++){
if(alc[0]!=c){
nalc[b]=alc[j];
b++;
}
}
*n=&a;
return nalc;
}
why do I keep having errors at the two for loops of the problem5?
it says something like comparison between pointer and int.
how can i fix that while the argument n remains pointer.
Use *n to access the value pointed by n:
for(int i = 0; i < *n; i++){
And to change the value pointed by n to be equal to a:
*n = a;
You can't have an address in a for-loop. Try dereferencing the pointer. and also post your exact error please.
Related
The purpose of the Copy function in the following code is to copy an array of integers from one array to another using C++ but the output seems wrong.
What could be the problem here?
#include <iostream>
using namespace std;
void Copy(int old_array[],int new_array[],int length)
{
int *ptr1 = old_array;
int *ptr2 = new_array;
int i = 0;
for(int i=0 ; i<length ; i++)
{
*(ptr2++) = *(ptr1++);
}
for (int i = 0; i <2; i++)
{
cout<<*(ptr2 + i)<<endl;
}
}
int main()
{
int a[2]={0,1};
int b[2];
Copy(a,b,2);
}
This is the output:
ptr2 is one past the end of the array when your print loop runs.
Try this:
void Copy(int old_array[], int new_array[], int length)
{
int* ptr1 = old_array;
int* ptr2 = new_array;
int i = 0;
for (int i = 0; i < length; i++)
{
*(ptr2++) = *(ptr1++);
}
ptr2 = new_array;
for (int i = 0; i < 2; i++)
{
cout << *(ptr2 + i) << endl;
}
}
Your ptr2 is pointing to the element b[2] (which is out-of-bound access) at the time you are printing it in the second for loop.
You can fix it by subtracting the length from the ptr2 in the second for loop like below.
#include <iostream>
using namespace std;
void Copy(int old_array[],int new_array[],int length)
{
int *ptr1 = old_array;
int *ptr2 = new_array;
int i = 0;
for(int i=0 ; i<length ; i++)
{
*(ptr2++) = *(ptr1++);
}
for (int i = 0; i <2; i++)
{
cout<<*(ptr2 + i - length)<<endl;
}
}
int main()
{
int a[2]={0,1};
int b[2];
Copy(a,b,2);
}
The copy seems fine but the second for is accessing ptr2 which was incremented in the first for and is point to some invalid memory position. You could use new_array in this second loop.
I suppose this second loop is only for debug and will be better located in the main using, in you case, the variable b.
I want to create a function which generates an array(filled with random numbers) of the size I give as an input and the function returns the address of the first element of the generated array. I wrote the code as best as possible without any errors or warning. But at the runtime, the program crashes. I try to debug it but the debugger also froze and do nothing. I think the problem is in returning the pointer. Please help.
#include<iostream>
#include<cstdlib>
using namespace std;
int** the_gen(int num)
{
srand(1000);
int *ptr= new int(num);
int** const dptr=&ptr;
for(int i=0;i<num;i++)
{
*ptr= rand();
ptr++;
}
return dptr;
}
int main()
{
cout<<"Size of array:"<<endl;
int size_of_array;
cin>>size_of_array;
int **a;
a=the_gen(size_of_array);
for(int i=0;i<size_of_array;i++)
{
cout<<**a<<",";
a++;
}
}
you were using int** unnecessarily. only need to use that if you're creating an array of int pointers or a 2d array of int's:
the following code does what you're after i think:
#include<iostream>
#include<cstdlib>
using namespace std;
int* the_gen(int num)
{
srand(1000);
//edit
int *ptr = new int[num];
int* const dptr = ptr;
for (int i = 0; i < num; i++)
{
*ptr = rand();
ptr++;
}
return dptr;
}
int main()
{
cout << "Size of array:" << endl;
int size_of_array;
cin >> size_of_array;
int *a;
a = the_gen(size_of_array);
for (int i = 0; i < size_of_array; i++)
{
cout << *a << ",";
a++;
}
}
I think returning pointer is always bad idea, we should take memory pointers as a parameter as the follow
void the_gen(int num, int** arry)
{
srand(1000);
int *ptr = new int[num];
*arry = ptr;
for (int i = 0; i < num; i++)
{
ptr[i] = rand();
}
}
int main()
{
cout << "Size of array:" << endl;
int size_of_array;
cin >> size_of_array;
int *a;
the_gen(size_of_array, &a);
for (int i = 0; i < size_of_array; i++)
{
cout << a[i] << ",";
}
}
Good evening, folks.
I'm currently experiencing difficulties with extracting pair numbers from an array. I have the following code:
#include <iostream>
using namespace std;
int *paire(int *d, int length) {
int counter = 0;
int position = 0;
for (int i=0; i<length; i++) {
if (d[i] % 2 ==0)
counter++;
}
int *k = new int[counter];
for (int i=0; i<length; i++) {
if (d[i] % 2 ==0) {
k[position] = d[i];
position++;
}
}
return k;
}
int main() {
int b[8] = {1,2,3,4,5,6,7,8};
int *array1 = paire(b,8);
for (int i=0; i<5; i++) { // how can I point here to the counter in paire() ?
cout<<array1[i];
}
delete[] array1;
return 0;
}
So I think I've got it right with initializing the new array in function paire, but I'm having difficulties to iterate through the array.
P.S. I'm first year in university, so I would really be thankful if you can keep the same simplicity in the answers. Thanks in advance!
It appears that you need to return 2 separate values: the number of even numbers in the array b, and the address of the newly allocated memory that is storing exclusively those even numbers.
Since you can not return multiple variables, one solution that does minimal modification to your code would be as follows.
int *paire(int *d, int length, int& counter) {
counter = 0;
// rest of your function remains unchanged
// ...
}
int main() {
int b[8] = {1,2,3,4,5,6,7,8};
int evenNumbers;
int *array1 = paire(b,8, evenNumbers);
for (int i=0; i<evenNumbers; i++) {
cout<<array1[i];
}
delete [] array1;
return 0;
}
Alternatively, you can return the value in counter and send the reference to the int* variable as an argument to paire function. Or, you can declare paire to have return type void and use references to pass back both the values.
You can further simplify your function by allocating to that of the length and returning the counter by an output parameter.
#include <iostream>
using namespace std;
int *paire(int *d, int length, int &counter) {
counter = 0;
int *k = new int[length]; // allocate for the maximum memory
for (int i = 0; i < length; ++i) {
if (d[i] % 2 == 0) {
k[counter++] = d[i];
}
}
return k;
}
int main() {
int b[8] = {1,2,3,4,5,6,7,8};
int counter = 0;
int *array1 = paire(b,8, counter);
for (int i=0; i<counter; i++) { // how can I point here to the counter in paire() ?
cout<<array1[i] << " ";
}
delete [] array1;
return 0;
}
But please note that as others have already pointed out this method is quite error prone in the sense that it leaves the responsibility to the client to delete the internal memory used by paire function.
I have to use pointers to copy values of one array to another. The problem is I'm not allowed to use'[ ]' operators, which makes this more difficult for me. Here is my attempt:
#include <iostream>
using namespace std;
void cpyia(int old_array[],int new_array[],int length){
int *p1 = old_array;
int *p2 = new_array;
int *x = p2;
for(int i=0 ; i<length ; i++){
p2 = x;
p2 = p2 + i;
p2 = p1 + i;
}
for(int i=0; i<5; ++i){
cout << p2[i] << endl;
}
}
int main() {
int a[5]={1,2,3,4,5};
int b[5];
cpyia(a, b, 5);
}
An easier way to do it would be to put p2[i] = p1[i] in the loop, but I cant do that. Any help is appreciated.
The standard way of implementing your function is as follow:
for(int i = 0; i < length; ++i)
*new_array++ = *old_array++;
To be a bit more explicit, it's the same as:
void cpyia(int old_array[],int new_array[],int length){
int *p1 = old_array;
int *p2 = new_array;
for(int i=0 ; i<length ; i++){
*(p2+i) = *(p1+i);
// another way: *(p2++) = *(p1++);
}
}
In real code, you would use std::copy before even thinking about rewriting such a simple thing yourself.
Here is a complete example:
#include <iostream>
#include <algorithm>
void cpyia(int old_array[],int new_array[],int length){
std::copy(old_array, old_array + length, new_array);
}
int main() {
int a[5]={1,2,3,4,5};
int b[5];
cpyia(a, b, 5);
// test results:
for (int index = 0; index < 5; ++index)
{
std::cout << a[index] << " <-> " << b[index] << "\n";
}
}
However, your question says that you are "not allowed to use" something, which sounds a lot like a homework assignment. In that case, you could look at possible implementations of std::copy to get an idea of how to do it. Here is one way:
void cpyia(int old_array[],int new_array[],int length){
int* first = old_array;
int* last = old_array + length;
int* d_first = new_array;
while (first != last) {
*d_first++ = *first++;
}
}
#include<iostream>
using namespace std;
int main() {
const int size = 5;
int arr1[size] = { 4,21,43,9,77 };
int arr2[size];
int *ptr_a = arr1;
int *ptr_b = arr2;
for (int i = 0; i < size; i++)
{
*(ptr_b + i) = *(ptr_a + i);
cout << *(ptr_b + i) << " ";
}
}
My complete program is as follows:
#include<iostream>
using namespace std;
int max(int *,int);
int main()
{
int n, a[10], b;
cout << "Please enter the no. of integers you wish to enter ";
cin >> n;
for(int i = 0; i < n; i++)
{
cout << endl << "please enter the " << i+1 << " no. ";
cin>>a[i];
}
b = max(&a[0], n);
cout << endl << "The greates no. is " << a[b] << " and its index position is " << b;
return 0;
}
int max(int * ptr,int n)
{
int b[10], i, max, k;
&b[0] = ptr;
max = b[0];
for(i = 0; i < n; i++)
{
if (max < b[i]);
max = b[i];
k = i;
}
return k;
}
I want to pass pointer to the function and find the greatest number.
I'm not sure if passing an array counts as passing pointers.
You don't need to allocate memory for b[10], you just need a pointer here, instead of
int b[10];
Just declare a pointer and set its address to the starting element of the array passed by the function.
ie
int* b= ptr;
#include<iostream>
using namespace std;
int max(int *,int);
int main()
{
int n,a[10],b;
cout<<"Please enter the no. of integers you wish to enter ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<endl<<"please enter the "<<i+1<<" no. ";
cin>>a[i];
}
b=max(a,n);
cout<<endl<<"The greates no. is "<<a[b]<<" and its index position is "<<b;
return 0;
}
int max(int *a,int n)
{
int i,max,k=0;
//&b[0]=ptr;
max=a[0];
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
k=i;
}
return k;
}
Try this program .
It does not use b[] , which is actually unnecessary , just pass array a as parameter .
CHANGES :
b=max(a,n);
int max(int *a,int n)
{
int i,max,k=0; // INITIALIZE k !
//&b[0]=ptr;
max=a[0];
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
k=i;
}
return k;
}
You should initialize K to 0 .
Your function is invalid You may not make assignment
&b[0] = ptr;
an such an assignment has no sense because it tries to change the address of array element b[0].
You need not to declare any additional array in the function.
Moreover your function has undefined beahviour in case then the first element is the maximum element of the array. In this case the function returns variable k that was not initialized.
Also after the if statement there is a semicolon
if (max < b[i]);
So this statement also has no sense.
The function can be written simpler
int max( const int * ptr, int n )
{
int max_i = 0;
for ( int i = 1; i < n; i++ )
{
if ( ptr[max_i] < ptr[i] ) max_i = i;
}
return max_i;
}
change your expression to :
b=max(a,n);
You need not pass array via reference, they are automatically passed by reference.
also change:
&b[0]=ptr; to b=ptr;
but for that initialize b as int * b;
or simply,
don't assign value of ptr to b, just directly work on ptr.