Can anyone please explain why the loop only execute once!!
The for loop executes once and never reaches end of the programm
and if you've got some time to review my mistakes then please point out mistakes because i know this is not how it is done!!
using namespace std;
#include<iostream>
int* rotate(int* ar,int d,int n)
{
int tmp[n];
d = d%n;
for (int i = 0; i < n; ++i)
{
tmp[i] = ar[(i+d)%n];
}
free(ar);
return tmp;
}
int main(int argc, char const *argv[])
{
int arr[] = {1,2,3,4,5};
int *a;
int n = sizeof(arr)/sizeof(arr[0]);
a = rotate(arr,4,n);
cout<<endl;
for (int i = 0; i < n; ++i)
{
cout<<i<<endl;
cout<<a[i]<<endl;
}
cout<<"End";
return 0;
}
Lets take it apart...
int* rotate(int* ar,int d,int n)
{
int tmp[n]; // 1
d = d%n;
for (int i = 0; i < n; ++i)
{
tmp[i] = ar[(i+d)%n];
}
free(ar); // 2
return tmp; // 3
}
int tmp[n]; is not standard C++. If you do not want to use std::vector the proper replacement would be a dynamically allocated array.
You call free with a pointer that was not allocated via malloc, which invokes undefined behavior. In C++ you shouldn't be using free and malloc at all, but rather new and delete. And also new and delete only for such exercise. Otherwise use smart pointers.
You return a pointer to a local variable. The pointer is dangling and using it in main invokes undefined behavior.
You can't simply replace the static array arr in main with something else in the function. A function called rotate is not expected to create a new array or delete the one that was passed (also because like in your case it is just not possible).
There are different ways to fix your code. I choose to make rotate rotate the array "in-place". However, as you can see, the implementation actually uses an additional array of same size. I leave it to you to figure out how to change it to use less additional memory:
#include <iostream>
using std::cout;
using std::endl;
void rotate(int* ar,int d,int n)
{
int* tmp = new int[n];
d = d%n;
for (int i = 0; i < n; ++i)
{
tmp[i] = ar[(i+d)%n];
}
for (int i = 0; i < n; ++i)
{
ar[i] = tmp[i];
}
}
int main(int argc, char const *argv[])
{
int arr[] = {1,2,3,4,5};
int n = sizeof(arr)/sizeof(arr[0]);
rotate(arr,4,n);
cout<<endl;
for (int i = 0; i < n; ++i)
{
cout<<i<<endl;
cout<<arr[i]<<endl;
}
cout<<"End";
return 0;
}
This is how you can do the same using std::rotate:
#include <array>
#include <iostream>
#include <algorithm>
int main(int argc, char const *argv[])
{
int arr[] = {1,2,3,4,5};
int n = sizeof(arr)/sizeof(arr[0]);
std::rotate(std::begin(arr),std::begin(arr)+4,std::end(arr));
for (int i = 0; i < n; ++i)
{
std::cout << i << std::endl;
std::cout << arr[i] << std::endl;
}
std::cout<<"End";
}
... it even uses pointers ;)
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.
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) << " ";
}
}
What I'm trying to do:
User inputs two numbers.
Array is declared using those numbers as dimensions.
Function outside main() is filling the array.
Array is accessed in main() for further thingies.
What I have problem with:
Function + array combination doesn't seem to work as I think.
What I did:
void tablica1(int h, int w)
{
int m,n;
for(m=0; m<h; m++)
for(n=0; n<w; n++)
{
arr[h][w]=1;
}
}
What happens:
array arr is inaccessible in tablica1() because it has not been declared in that function.
Of course, when I declare the array in tablica1() it becomes inaccessible in main().
Possible solutions:
Passing arr to tablica1() as a reference - no idea how to do that
Declaring arr in tablica1() and somehow passing it to main() - no idea how to do that
Other possible solutions?
You can declare the array outside of both, at compilation unit level:
int arr[10][10];
void func() {
for (int i=0; i<10; i++) {
for (int j=0; j<10; j++) {
arr[i][j] = i + j;
}
}
}
int main(int argc, const char *argv[]) {
func();
std::cout << arr[3][4] << "\n"; // Output will be 7
return 0;
}
If you want handle a dynamically-sized matrix the most common pattern is to use an std::vector of std::vectors (it's a little more general and therefore a little less efficient than a 2d matrix because each row can have a different length, but in most cases the cost difference is not a big issue)
#include <vector>
std::vector< std::vector< int > > arr;
void func() {
int height = arr.size();
int width = arr[0].size();
for (int i=0; i<height; i++) {
for (int j=0; j<width; j++) {
arr[i][j] = i + j;
}
}
}
int main() {
int height = 13;
int width = 7;
arr = std::vector< std::vector<int> >(height, std::vector<int>(width));
func();
...
}
the two solutions you mentioned
1、Passing arr to tablica1() as a reference
void tablica1(int h, int w,int **arr)
{
int m,n;
for(m=0; m<h; m++)
for(n=0; n<w; n++)
{
arr[h][w]=1;
}
}
void main()
{
const int h=100,w=100;
int arr[h][w];
tablica1(h,w,arr);
}
2、Declaring arr in tablica1() and somehow passing it to main()
int **tablica1(int h, int w)
{
int m,n;
int **arr=new int*[h];
for(int i=0;i<h;i++)
{
arr[i]=new int[w];
}
//it is best to initialize arr by setting each element 0
for(m=0; m<h; m++)
for(n=0; n<w; n++)
{
arr[h][w]=1;
}
return arr;
}
void main()
{
const int h=100,w=100;
int **arr=tablica1(h,w);
//do somting
//delete arr
for(int i=0;i<h;i++)
{
delete []arr[i];
}
delete []arr;
}
If you want to declare a dynamic multidimensional array you can do that with the template give below.
#include<iostream.
#include <vector>
using namespace std;
typedef vector<int> vi;
vector<vi> arr; // arr is a dynamic two dimensional array.
vi.assign(10,vi());//10 rows of type vi .
If you want to enter values in arr you can do that by
vi[0].push_back(a);
vi[0].push_back(b); // a,b,c are some example values..
vi[1].push_back(c);
You can understand using this code
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
typedef vector <int> vi;
vector<vi> arr;// dynamic multidimensional array
int main(){
cout<<"enter array size\n";
int h,w,temp;;
cin>>h>>w;
arr.assign(h,vi());
int i, j;
for(i=0; i < h ;i++)
for(j=0; j < w; j++)
{
cin>>temp;
arr[i].push_back(temp);
}
// for printing
for(i=0; i < h ;i++){
for(j=0; j < w; j++)
{
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
I got a problem with the following code:
#include <iostream>
using namespace std;
double* FillArray(void)
{
double result[5];
for (int i = 0; i<5;i++){
result[i]=(double) i;
}
return result; // return the pointer
}
int main()
{
double * a = FillArray();
for (int i = 0; i<5;i++){
cout << a[i] << endl; // print out the array
}
return 0;
}
The outputs are strange:
0
3.47187e-236
8.89753e-308
8.8976e-308
3.90251e-236
Could you tell what wrong in my code?
I tried to use a function to return an array, and print out it in the main().
You are returning a pointer to a local variable, the array result. This is undefined behaviour. The variable ceases to exist when the function returns, so by the time you get to printing, you print garbage values.
If you really want to return an array, you can use an std::array:
typedef std::array<double, 5> DArray5;
DArray5 FillArray()
{
DArray5 result;
for (size_t i = 0; i < result.size(); ++i){
result[i] = i;
}
return result;
}
Edit This is a C++ only answer. There is no C/C++ language.
result is allocated in the stack. Try allocating it in the heap, like this:
double* result = new double[5];
The array called result get released. Try this code:
#include <iostream>
using namespace std;
void FillArray(double *result)
{
for (int i = 0; i<5;i++){
result[i]=(double) i;
}
}
int main()
{
double result[5];
FillArray(result);
for (int i = 0; i<5;i++){
cout << result[i] << endl; // print out the array
}
return 0;
}
This is WRONG:
double* FillArray(void)
{
// Result ONLY has scope INSIDE of the FillArray function
double result[5];
for (int i = 0; i<5;i++){
result[i]=(double) i;
}
// Bad things will happen when the caller tries to use (now-invalid) "result"
return result;
Here is one correct alternative:
#define ARRAY_SIZE 5
main ()
double result[ARRAY_SIZE];
FillArray (result, ARRAY_SIZE);
for (int i = 0; i<ARRAY_SIZE;i++){
cout << result[i] << endl; // print out the array
}
...
void FillArray(double *array, int n)
{
for (int i = 0; i<n;i++){
array[i]=(double) i;