Hello I have a task to make a function that fills an array, made in main(), and then use the array in another function, where I should make some calculations with it. I already have some code, could you please assist me how to do it? Thank you in advance
#include <iostream>;
using namespace std;
int* modArray(int* a, int size) {
for (int i = 0; i < size ; i++) {
a[i] = i;
}
return a;
}
int modAddition() {} // This is the function, where I want to use, the "a" array
int main() {
int size = 7;
int* a = new int[size];
a = modArray(a, size);
system("pause");
return 0;
}
Your modArray works, and the return steatment is not needed in this case. You can just do the same in your modAddition. Rembemer that if they don't have a return, they will be void methods.
Related
I am new to C++. I am trying to define a binary converter function and return a pointer. Then U want to display generated binary in the main function:
#include <iostream>
using namespace std;
int* binary_con(int n)
{
int i;
int binary[100];
for (i = 0; n > 0; i++)
{
binary[i] = n % 2;
n = n / 2;
}
return binary;
}
int main()
{
int n;
int* a;
cout << "Input the number:";
cin >> n;
a = binary_con(n);
while (*a)
{
cout << *a;
a++;
}
return 0;
}
But after I run my code, I got this:
Can anyone explain this to me?
you can't return an array from a function, the way is to pass that array as an argument to the functionm using this approach:
void binary_con(int n, int *binary)
now you have access to binary array inside your function, hence you can edit it and see the changes outside of the function without returning anything.
inside your main, instead of writing a = binary_con(n);, you should write this:
binary_con(n, a);
I'm having an issue with the pointer return function. The error, "calling object type 'int* ' is not a function or a function pointer" reverseArray = reverseArray(array,size);.I am not sure why it's giving me this error, this is not my solution I'm using this solution as a guidance to help me solve the problem. Since I've sat for now 2 hours trying to solve it and I got no where with it, so I decide to look it up and get an idea on how to approach the problem. And break down their solution by using a debugger to see why their solution works. I know it's a bad thing to do because I'm not learning how to solve problems on my own.
#include <iostream>
int* reverseArray(int [], int );
int main()
{ const int size =5;
int array[size] = {1,2,3,4,5};
int* reverseArray;
for(int i =0; i < size;i++)
{
std::cout << array[i];
}
reverseArray = reverseArray(array,size);
for(int i =0; i <size;i++)
{
std::cout <<array[i];
}
return 0;
}
int* reverseArray(int array [],int size)
{
int* newArray;
newArray = new int[size];
int j = 0;
for(int k =size-1;k>=0;k--)
{
newArray[j] = array[k];
j++;
}
return newArray;
}
The error is self explanatory.
"calling object type 'int* ' is not a function or a function pointer". In you main() function, you named the array you want to pass as a parameter to your reverseArray() function with the same name as your function (reverseArray). The compiler get confused within that scope, because of this and thinks you're calling a variable as a function.
See below:
#include <iostream>
int* reverseArray(int [], int );
int main()
{ const int size =5;
int array[size] = {1,2,3,4,5};
int* reverseArray; // Change this name to something else
for(int i =0; i < size;i++)
{
std::cout << array[i];
}
reverseArray = reverseArray(array,size);
for(int i =0; i <size;i++)
{
std::cout <<array[i];
}
return 0;
}
Hope it helps :)
I get an error when I try to call my member function to copy the array into another array. Im not sure if I am calling it wrong or what. I think I have the syntax right on most parts but I am also not sure if it matter if the member function is a void or int.
Main.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "Class.h"
using namespace std;
int main()
{
// Max size of array
int MaxRange = 1000;
// Get System time
unsigned seed = time(0);
// seed random number generator
srand(seed);
// allocate memory for array
int * Array = new int[1000];
int * CopiedArray = new int[1000];
// Randomly generate numbers into array
for (int i = 0; i < 1000; i++)
{
Array[i] = 1 + rand() % MaxRange;
}
//print array
for (int j = 0; j < 1000; j++)
{
cout << Array[j] << endl;
}
CopiedArray = Sort.CopyArray(Array);
return 0;
}
Class.h
#include <iostream>
using namespace std;
class Sort
{
public:
void CopyArray(int * Array);
};
Class.cpp
#include <iostream>
#include "Class.h"
using namespace std;
void CopyArray::CopyArray(int * Array)
{
// Allocate memory for copied array
int * CopiedArray = new int[1000]
//copy date to array
for(int i = 0; i < 1000; i++)
{
CopiedArray[i] = Array[i]
}
cout << " THIS IS THE COPIED ARRAY" << endl;
// print copied array
for (int j = 0; i < 1000; i++)
{
cout << CopiedArray[j] << endl;
}
}
in your example you are accessing a member function CopyArray without an object you cannot do that. you must create an object of class Sort then use it to access the members. otherwise make CopyArray static and then change it to
class Sort
{
public:
static int* CopyArray(int* Array); // to access this function just use the name of class and `::`
// int* CopyArray(int* Array); // to access this function you must have an object of Sort class
};
int* Sort::CopyArray(int * Array)
{
int * CopiedArray = new int[1000]; // semicolon missin
// your code processing here
return CopiedArray;
}
int main()
{
CopiedArray = Sort::CopyArray(Array); // accessing the static member function `CopyArray`
// or you can create an object of Sort class but you must also make Copyarray non-static to be able to:
// Sort theSort;
// CopiedArray = theSort.CopyArray(Array);
return 0;
}
* also in your example you are assigning a void to a pointer to int:
CopiedArray = Sort.CopyArray(Array);// because in your example CopyArray returns void.
CopiedArray = Sort.CopyArray(Array);
however this function is defined as void
void CopyArray::CopyArray(int * Array)
You can not set a pointer to the result of a void function.
And of course you did not declare in instance of Sort and CopyArray is not staic like #Bim mentioned.
You should also not change the value of CopiedArray because it was allocated and you need to free it.
Sorry your code is really a mess.
It says:
[Error] invalid conversion from 'int*' to 'int' [-fpermissive] on line 9 col 5.
What was asked of me to do:
Make a program that would accept array of 10 integers and determine the highest and the lowest integers from the set of integers. Use pointer variables for the highest and lowest integer.
what i did:
#include<iostream>
using namespace std;
int main()
{
int kre_arr[10];
int *kre_p;
for(int k = 0; k<=10; k++)
{
kre_p[k] = &kre_arr[k];
}
int j,temp;
cout<<"Enter 10 Integers: ";
for (*kre_p=0; *kre_p < 10; *kre_p++)
{
cin>>kre_arr[*kre_p];
}
for(*kre_p=0;*kre_p<=10;*kre_p++)
{
for(j=*kre_p+1;j<=10;j++)
{
if(kre_arr[*kre_p] > kre_arr[j])
{
temp = kre_arr[*kre_p];
kre_arr[*kre_p] = kre_arr[j];
kre_arr[j] = temp;
}
}
}
for(*kre_p=0;*kre_p<=9;*kre_p++)
{
cout<<endl<<kre_arr[*kre_p];
}
}
code i did before adding pointer i dont seem to understand pointer that much.
#include<iostream>
using namespace std;
int main()
{
int kre_arr[10];
int *kre_p;
int i,j,temp;
cout<<"Enter 10 Integers: ";
for (int i=0; i < 10; i++)
{
cin>>kre_arr[i];
}
for(i=0;i<=10;i++)
{
for(j=i+1;j<=10;j++)
{
if(kre_arr[i] > kre_arr[j])
{
temp = kre_arr[i];
kre_arr[i] = kre_arr[j];
kre_arr[j] = temp;
}
}
}
for(i=0;i<=9;i++)
{
cout<<endl<<kre_arr[i];
}
}
Looking at what you are asked to do I think you just have to determine the highest and lowest int in the array and point to. You sort the array thats slower.
I think it should look like that:
#include<iostream>
using namespace std;
int main()
{
int kre_arr[10];
int *low;
int *high;
cout<<"Enter 10 Integers: ";
for (int i=0; i < 10; i++)
{
cin>>kre_arr[i];
}
//determine the lowest
low=&kre_arr[0];
for(int i=1;i<10;i++)
{
if(kre_arr[i] < *low)
{
low=&kre_arr[i];
}
}
//determine the highest
high=&kre_arr[0];
for(int i=1;i<10;i++)
{
if(kre_arr[i] > *high)
{
high=&kre_arr[i];
}
}
cout<<"lowest: "<<*low<<"\nhighest: "<<*high;
}
kre_p[k] = &kre_arr[k];
kre_arr is array.
kre_arr[k] is integer.
&kre_arr[k] is integer address ( similar int*)
kre_p is pointer.
kre_p[k] is integer.
So, as a result, you cannot pass directly int* to int.
I guess you want kre_p+k = &kre_arr[k]
Given the state of your code, I fear for your life... So, for your overal survival, and of course in the hopes that you will learn something:
Never use 'using namespace std'. It's bad form.
You are not allocating memory for your array of pointers (kre_p). That will cause your program to crash for sure.
You don't actually need an array of pointers. Your array elements can be conveniently referred to by their offset in the array.
You are doing what appears to be a bubblesort to find the lowest and highest value. That's incredibly inefficient, and completely unnecessary.
C++ can be such a nice language. It bothers me when teachers seem to think they should be teaching it in a form that's as ugly as possible. Consider:
#include <algorithm>
#include <array>
#include <iostream>
int main () {
std::cout << "Enter 10 Integers: ";
std::array<int, 10> kre_arr;
for (auto &Val : kre_arr)
std::cin >> Val;
const int Low = *std::min_element (kre_arr.begin (), kre_arr.end ());
const int High = *std::max_element (kre_arr.begin (), kre_arr.end ());
// The assignment calls for pointers, so let's not disappoint.
const int *LowPtr = &Low;
const int *HighPtr = &High;
}
My weekend assignment was to make a function that gets an array of integers and the size of the array, and creates an array of pointers so that the pointers will be sorted using bubble sort (without changing the original array).
While debugging I found out that it works just fine, but when the function goes back to main() the pointers array gets initialized and everything's gone.
#include <iostream>
using namespace std;
void pointerSort(int arr[], int size, int* pointers[]);
void swap(int a, int b);
void main()
{
int arr[5]={7,2,5,9,4};
int size = 5;
int* pointers[5];
pointerSort(arr, size, pointers);
for (int i = 0; i < 5 ; i++)
cout << *pointers[i] << endl;
}
void pointerSort(int arr[], int size, int* pointers[])
{
int j, i;
bool change = true;
pointers = new int*[size];
for (i = 0; i < size; i++)
pointers[i] = &arr[i];
i = 0;
j = 1;
while (i <= size-1 && change == true)
{
change = false;
for (i = 0; i < size-j; i++)
{
if (*pointers[i] > *pointers[i+1])
{
swap(pointers[i], pointers[i+1]);
change = true;
}
}
j++;
}
}
void swap(int&a, int&b)
{
int temp;
temp = a;
a = b;
b = temp;
}
pointers = new int*[size];
At this point pointers is already an array of pointers, no allocation is needed.
After this line pointers IS NO LONGER THE ARRAY IN YOUR MAIN FUNCTION.
This is why your function is failing, because you are reassigning the array to which pointers is pointing to. The original array ISNT getting reinitialized, its just ignored throughout the entire code.
It is also a memory leak as ATaylor mentions, since you do not delete the allocated space, and cannot delete the space after the function finishes.
To fix everything: just remove the above line.