Is there a way to copy an array to another way in reverse order by using a while loop in c++??
I'm pretty sure I know how to do one with a for loop, but I'm curious if anyone knows of a way by using a while loop
Why not something like this?
#include <algorithm>
int src[] = {1, 2, 3, 4, 5};
int dst[5];
std::reverse_copy(src, src+5, dst);
int anArray = {1, 2, 3, 4, 5};
int reverseArray[5];
int count = 4;
int place = 0;
while(place < 5) {
reverseArray[place] = anArray[count];
count--;
place++;
}
As you said that you have done using for loop, you can follow following steps to convert it to while loop.
for(int i = sizeof(arr) - 1; i >= 0; i--)
{
// your logic
}
now convert it to,
int i = sizeof(arr);
for(; i >= 0; )
{
// your logic
i--;
}
simply replace for with while and remove ; within the braces.
int i = sizeof(arr);
while(i >= 0)
{
// your logic
i--;
}
You can use std::reverse for reversing the same array and std::reverse_copy for reversing to another output array as:
int main() {
int a[]= {1,2,3,4,5,6,7,8,9,10};
const int size = sizeof(a)/sizeof(int);
int b[size];
//copying reverse to another array
reverse_copy(a, a + size, b);
cout << "b = {";
copy(b, b + size, ostream_iterator<int>(cout, ", "));
cout << "}" << endl;
//reverse the same array
reverse(a, a + size);
cout << "a = {";
copy(a, a + size, ostream_iterator<int>(cout, ", "));
cout << "}" << endl;
return 0;
}
Output:
b = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, }
a = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, }
Demo : http://www.ideone.com/Fe5uj
There's been a few questions similar to this recently. I wonder if it is homework or an interview question somewhere. Here's one answer:
#define ELEMENT_COUNT(a) (sizeof((a))/sizeof((a)[0]))
int anArray[] = { 1, 2, 3, 4, 5 };
int reverseArray[ELEMENT_COUNT(anArray)];
int n = ELEMENT_COUNT(anArray);
int i = 0;
while(n--)
reverseArray[i++] = anArray[n];
I think it might be probing to see if you understand when expression like i++ and n-- are evaluated.
Related
I'm a newbie programmer trying to solve the following problem: I need to initialize a matrix with all the combinations from a array of objects so I can extract the values and perform certain calculations afterwards for each set of objects, in this case I used a struct for X, Y coordinates to represent the data. The entire data set consists on 35 coordinates, for now I'm dealing with as few data as possible, an input array of size 4, meaning 4 (n) combinations of 3 (r) objects. The program seems to work fine until I print the 4x3 matrix and find out I was only able to store the first combination, and after tinkering with the program I got stuck since I didn't code this program entirely. Could someone suggest me a solution so the matrix gets initialized correctly? I'd highly appreciate it.
#include <iostream>
#define n 4 //data set size
#define r 3 // combination size
using namespace std;
struct Points{
double x, y;
};
void Combination(Points Data [n], Points CombinationMatrix [][r],int start, int currLen, bool check []) {
// Return if the currLen is more than the required length.
if(currLen > r)
return;
// If currLen is equal to required length then add the sequence.
else if (currLen == r){
for (int i = 0; i < n; i++){
for(int j = 0; j < r; j++){
if (check[i] == true){
CombinationMatrix[i][j] = Data[j];
}
}
}
return;
}
// If start equals to len then return since no further element left.
if (start == n)
{
return;
}
// For every index we have two options.
// First is, we select it, means put true in check[] and increment currLen and start.
check[start] = true;
Combination(Data, CombinationMatrix, start + 1, currLen + 1, check);
// Second is, we don't select it, means put false in check[] and only start incremented.
check[start] = false;
Combination(Data, CombinationMatrix, start + 1, currLen, check);
}
int main()
{
Points Data [n] = { {1, 1} , {2, 7} , {3, 6} , {4, 13}}; //, {5,9} ,
//{6, 7} , {7, 12} , {8, 14} , {9, 17} , {10, 23} ,
//{11,28} , {12, 63} , {13, 45} , {14, 68} , {15, 32} ,
//{16,98} , {17, 115} , {18, 116}, {19, 112}, {20, 115},
//{21, 88} , {22, 86} , {23, 106}, {24, 136}, {25, 158},
//{26, 198}, {27, 128} , {28, 187}, {29, 112}, {30, 149},
//{31, 279}, {32, 224} , {33, 222}, {34, 260}, {35, 166}};
Points CombinationMatrix [n][r];
bool check[n];
for(int i = 0; i < n; i++){
check[i] = false;
}
Combination(Data, CombinationMatrix, 0, 0, check);
for (int i = 0; i < n; i++){
for(int j = 0; j < r; j++){
cout << CombinationMatrix[i][j].x << "," << CombinationMatrix[i][j].y << " ";
}
cout << endl;
}
return 0;
}
I suggest that you have a look at std::prev_permutation.
If you have a std::vector<Data> points, then you can get all permutations via
do {
// Do something with the current permutation
for ( int i = 0; i < points.size(); ++i ) {
std::cout << points[i] << ' ';
}
std::cout << '\n';
} while ( std::prev_permutation(points.begin(),points.end()) );
Since this gives you points.size()! (Factorial) combinations, I would not store it in a matrix unless you have a very good reason to do so.
std::prev_permutation uses the lexicographically smaller. Thus, you need to overload the operator < for Data.
inline bool operator< (const Data& lhs, const Data& rhs){
/* do actual comparison e.g.*/
return ((lhs.x <rhs.x) && (lhs.y <rhs.y));
}
The following generates combinations of your array using std::prev_permutation.
Note that this is accomplished by using a bool vector that starts with r of those bits set to true, and on each iteration the bits in the bool vector have their positions changed.
The following uses std::vector<Point> instead of hard-coded arrays. This adds flexibility in that you don't have to guess how many combinations will be generated.
#include <iostream>
#include <vector>
#include <algorithm>
struct Points {
double x, y;
};
std::vector<std::vector<Points>> Combination(std::vector<Points>& Data, int n, int r)
{
// The returned vector
std::vector<std::vector<Points>> retVect;
// Array of bools
std::vector<bool> bits(n);
// Fill the first r positions of the bool array to true
std::fill(bits.begin(), bits.begin() + r, true);
// Our temporary 1 dimensional array we use when building a single combination
std::vector<Points> tempV;
do
{
tempV.clear();
for (int i = 0; i < n; ++i)
{
// for each item in the bool array that's true, add that to the vector
if (bits[i])
tempV.push_back(Data[i]);
}
// add this combination to vector of combinations
retVect.push_back(tempV);
// rearrange the bits
} while (std::prev_permutation(bits.begin(), bits.end()));
return retVect;
}
int main()
{
std::vector<Points> Data = { {1, 1}, {2, 7}, {3, 6}, {4, 13} };
auto CombinationMatrix = Combination(Data, 4, 3);
for (size_t i = 0; i < CombinationMatrix.size(); i++) {
for (size_t j = 0; j < CombinationMatrix[i].size(); j++) {
std::cout << "{" << CombinationMatrix[i][j].x << "," << CombinationMatrix[i][j].y << "} "; }
std::cout << std::endl;
}
}
Output:
{1,1} {2,7} {3,6}
{1,1} {2,7} {4,13}
{1,1} {3,6} {4,13}
{2,7} {3,6} {4,13}
Two quantities u and v are said to be at right angles if
nuv = u1v1 + u2v2 + u3v3 + u4v4 +………… + unvn = 0
Write a function that computes whether u and v are at right angles. You may use arrays if you wish. The function can assume that the vectors have the same dimension (n, say), but this number should be passed as a parameter to the function.
I have a few errors in my program. I'd appreciate some help, as I'm a beginner. The errors are telling me:
In function 'void function(int*,int*)'
cpp 26: error expected ';' before '}' token
cpp 29: error ;value required as left operand of assignment
#include <iostream>
using namespace std;
const int n = 5;
void function(int array[n],int array2[n]);
int main(){
int array[n] = {5, 3 , -4, 2, 8};
int array2[n] ={-7, -9, 5, 2, 9};
function(array, array2);
return 0;
}
void function(int array[n], int array2[n]){
int multiple;
for(int i=0; i <=5, i++)
{
(array[i]*array2[i]) + (array[i+1]*array2[i+1]) = multiple;
}
cout << multiple << endl;
}
Your for loop is malformed. You need to use < instead of <=, use n instead of 5, and use ; instead of ,.
Your assignment of multiple is backwards. The value on the right-side of the = operator is assigned to the variable on the left-side of =. You are trying to assign the value of multiple (which is uninitialized) to a dynamically computed value that has no explicit variable of its own. You should be assigning the computed value to multiple instead.
Also, you didn't follow the "this number [array dimensions] should be passed as a parameter to the function" requirement of your instructions.
Try this:
#include <iostream>
using namespace std;
const int n = 5;
void function(const int *array1, const int *array2, const int size);
int main()
{
int array1[n] = { 5, 3, -4, 2, 8};
int array2[n] = {-7, -9, 5, 2, 9};
function(array1, array2, n);
return 0;
}
void function(const int *array1, const int *array2, const int size)
{
int multiple = 0;
for(int i = 0; i < size; i++)
{
multiple += (array1[i] * array2[i]);
}
cout << multiple << endl;
}
Syntax error is where you are using the for loop in this fashion:
for(int i=0;i<=5,i++)
Use this instead:
for(int i=0; i <= 5; i++)
The function can assume that the vectors have the same dimension (n,
say), but this number should be passed as a parameter to the
function.
This function declaration
void function(int array[n],int array2[n]);
does not include a parameter that specifies the dimension of the arrays.
The above declaration is equivalent to
void function(int *array,int *array2);
because arrays passed by value are implicitly converted to pointers to their first elements.
There are a typo and a bug in this for statement
for(int i=0; i <=5, i++)
^^^^^^
There shall be
for ( int i=0; i < n; i++)
The variable multiple
int multiple;
is not initialized and this assignment
(array[i]*array2[i]) + (array[i+1]*array2[i+1]) = multiple;
does not have sense and has nothing common with the condition
nuv = u1v1 + u2v2 + u3v3 + u4v4 +………… + unvn = 0
It seems what you mean is the following
#include <iostream>
bool function( const int array[], const int array2[], size_t n )
{
long long int product = 0;
for ( size_t i = 0; i < n; i++)
{
product += array[i] * array2[i];
}
return product == 0;
}
int main()
{
const size_t N = 5;
int array[N] = { 5, 3 , -4, 2, 8 };
int array2[N] = { -7, -9, 5, 2, 9 };
std::cout << "array and array2 are "
<< (function(array, array2, N) ? "" : "not ")
<< "at right angles"
<< std::endl;
return 0;
}
These arrays
int array[N] = { 5, 3 , -4, 2, 8 };
int array2[N] = { -7, -9, 5, 2, 9 };
are not a right angles,
But these arrays
int array[N] = { 5, 3 , -4, 1, 9 };
int array2[N] = { -7, -9, 5, 1, 9 };
are at right angles. Try them.
Alternative: try the C++ way. Use std::array that knows it's length. Use algorithms provided by the standard library like std::inner_product.
#include <iostream>
#include <algorithm>
#include <array>
#include <numeric>
int main()
{
using arr_t = std::array<int,5>;
arr_t arr1 = {5, 3 , -4, 2, 8};
arr_t arr2 = {-7, -9, 5, 2, 9};
int mult = std::inner_product( begin(arr1), end(arr1), begin(arr2), 0,
std::plus<>(), std::multiplies<>() );
std::cerr << mult << "\n";
}
I'm trying to convert some old C functions to C++. My original programme stores a matrix in a single array, and I just pass a pointer to the first element to a function so that I am working on the correct row, e.g.
double f1(int *a){
return a[0] + a[1];
}
int main(void){
int x[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for(i = 0; i < 5; i++){
printf("%d\n", f1(&x[2 * i]));
}
I would like to be able to do something similar using the STL without copying. So my programme would look something like this
double f1(vector<int>& a){
return a[0] + a[1];
}
int main(void){
int x[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector<int> y(x, x + 10);
for(i = 0; i < 5; i++){
cout << f1(y) << endl; // This is clearly wrong
}
How would I do this? I could change my function to receive a reference to a vector::iterator I guess, but is there another way?
You could simply pass an iterator to the function. Random access iterators are very similar to pointers (in fact, pointers qualify as random access iterators.) For example,
#include <vector>
double f1(std::vector<int>::const_iterator a)
{
return a[0] + a[1];
}
#include <iostream>
int main()
{
vector<int> y{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto it = y.cbegin();
for(int i = 0; i < y.size()/2; ++i)
std::cout << f1(it + 2*i) <<std::endl;
}
Write array view. An array view is a pair of pointers with begin end size empty, operator[], front and back methods, and constructors from C arrays, std::array<T,N>&, std::vector<T,A>&, std::vector<non_const_T,A>const&, std::array<non_const_T,N>const&, std::initializer_list<non_const_T>, etc.
Oh, and T*,size_t and T*,T* ctors, which are great for slicing (use forwarding ctors: T*,size_t->T*,T*, and everything else to those 2).
It does not own its data, so all of its methods are const except operator=. (non-const methods would be methods that change the view range -- changing the elements is a const operation on a view).
Then
double f1(array_view<const int> a){
return a[0] + a[1];
}
You don't need to make so many changes:
double f1(int *a)
{
return a[0] + a[1];
}
int main(void)
{
vector<int> y = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int i = 0; i < 5; i++) {
cout << f1(&y[2 * i]) << endl;
}
}
I am trying to split an array, and this is how I am doing it:
int arr1[] = {1, 2, 3, 4, 5};
int *arr2 = arr1 + 1;
Now I need to do this in a loop. In every iteration, I am trying to decrease the size of the array by 1 element or 2 elements based on a condition. For obvious reasons I can't declare int arr2[some_variable] .
But I want a new array to be created in every iteration of the loop whose size is 1 less than its parent array. I am not sure how I can achieve this. Can anyone help please?
In Java, there is a function which can do this: int newArr[] = Arrays.copyOfRange(arr, 1, arr.length); I wanted something similar to this in C++.
Use std::vector<int> from the C++ standard library:
#include <string>
#include <iostream>
#include <vector>
void dump(std::vector<int> &v)
{
for (std::vector<int>::iterator it = v.begin(); it != v.end(); it++)
std::cout << *it << " ";
}
int main()
{
int a[] = { 1, 2, 3, 4, 5 };
std::vector<int> v(a, a + 5);
std::vector<int> v2(v.begin() + 1, v.begin() + 4);
dump(v);
std::cout << std::endl;
dump(v2);
}
And if you really-really-really can't use vector (why, seriously?), then just memcpy.
int arr[] = { 1, 2, 3, 4, 5 };
int cp[3];
memcpy(cp, arr + 1, sizeof(cp));
I'm not sure exactly why you want to have a new one each time, but you can use a standard container, like std::vector:
std::vector<int> arr1{1, 2, 3, 4, 5}; //std::iota is another option
int index{};
/*loop header*/ {
//make sure arr1.size() is at least 1
std::vector<int> arr2(std::next(std::begin(arr1), ++index), std::end(arr1));
}
I use std::next because it works in more scenarios. All this does is create a vector from two iterators: as far past the beginning as necessary, and the end (one past because it's exclusive).
Though C++11 is nice, it's not always a reality. In that case, this should work:
int arr1temp[] = {1, 2, 3, 4, 5};
std::vector<int> arr1(arr1temp, arr1temp + sizeof(arr1temp)/sizeof(arr1temp[0]));
int index = 0;
/*loop header*/ {
//make sure arr1.size() is at least 1
std::vector<int>::iterator start = arr1.begin();
std::advance(start, ++index);
std::vector<int> arr2(start, arr1.end());
}
/why/ do you think you need to make a copy of the array? Pointers and arrays are somewhat interchangeable, so all you need to do is track size and modify that.
void doSomethingWithArray(int* array, size_t arraySize) {
....
}
const size_t arraySize = 5;
int arry1[arraySize] = { 1, 2, 3, 4, 5 };
int* array = arr1;
int* arrayEnd = arr1 + arraySize;
for (int i = 0; i < 10; ++i) {
if ((*array) & 1 == 1) {
array += 1;
} else {
array += 3;
}
if(array >= arrayEnd)
break;
size_t howBigIsMySlice = arrayEnd - array;
if(howBigIsMySlice > && (rand() % 1) == 0)
howBigIsMySlice -= 1;
doSomethingWithArray(array, howBigIsMySlice);
}
You made no copies of data, and as far as "doSomethingWithMyArray" is concerned, the array is as big as we tell it.
Pseudo Code:
int arr[ 5 ] = { 4, 1, 3, 2, 6 }, x;
x = find(3).arr ;
x would then return 2.
The syntax you have there for your function doesn't make sense (why would the return value have a member called arr?).
To find the index, use std::distance and std::find from the <algorithm> header.
int x = std::distance(arr, std::find(arr, arr + 5, 3));
Or you can make it into a more generic function:
template <typename Iter>
size_t index_of(Iter first, Iter last, typename const std::iterator_traits<Iter>::value_type& x)
{
size_t i = 0;
while (first != last && *first != x)
++first, ++i;
return i;
}
Here, I'm returning the length of the sequence if the value is not found (which is consistent with the way the STL algorithms return the last iterator). Depending on your taste, you may wish to use some other form of failure reporting.
In your case, you would use it like so:
size_t x = index_of(arr, arr + 5, 3);
Here is a very simple way to do it by hand. You could also use the <algorithm>, as Peter suggests.
#include <iostream>
int find(int arr[], int len, int seek)
{
for (int i = 0; i < len; ++i)
{
if (arr[i] == seek) return i;
}
return -1;
}
int main()
{
int arr[ 5 ] = { 4, 1, 3, 2, 6 };
int x = find(arr,5,3);
std::cout << x << std::endl;
}
The fancy answer:
Use std::vector and search with std::find
The simple answer
Use a for loop
If the array is unsorted, you will need to use linear search.
#include <vector>
#include <algorithm>
int main()
{
int arr[5] = {4, 1, 3, 2, 6};
int x = -1;
std::vector<int> testVector(arr, arr + sizeof(arr) / sizeof(int) );
std::vector<int>::iterator it = std::find(testVector.begin(), testVector.end(), 3);
if (it != testVector.end())
{
x = it - testVector.begin();
}
return 0;
}
Or you can just build a vector in a normal way, without creating it from an array of ints and then use the same solution as shown in my example.
int arr[5] = {4, 1, 3, 2, 6};
vector<int> vec;
int i =0;
int no_to_be_found;
cin >> no_to_be_found;
while(i != 4)
{
vec.push_back(arr[i]);
i++;
}
cout << find(vec.begin(),vec.end(),no_to_be_found) - vec.begin();
We here use simply linear search. At first initialize the index equal to -1 . Then search the array , if found the assign the index value in index variable and break. Otherwise, index = -1.
int find(int arr[], int n, int key)
{
int index = -1;
for(int i=0; i<n; i++)
{
if(arr[i]==key)
{
index=i;
break;
}
}
return index;
}
int main()
{
int arr[ 5 ] = { 4, 1, 3, 2, 6 };
int n = sizeof(arr)/sizeof(arr[0]);
int x = find(arr ,n, 3);
cout<<x<<endl;
return 0;
}
You could use the STL algorithm library's find function provided
#include <iostream>
#include <algorithm>
using std::iostream;
using std::find;
int main() {
int length = 10;
int arr[length] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int* found_pos = find(arr, arr + length, 5);
if(found_pos != (arr + length)) {
// found
cout << "Found: " << *found_pos << endl;
}
else {
// not found
cout << "Not Found." << endl;
}
return 0;
}
There is a find(...) function to find an element in an array which returns an iterator to that element. If the element is not found, the iterator point to the end of array.
In case the element is found, we can simply calculate the distance of the iterator from the beginning of the array to get the index of that element.
#include <iterator>
using namespace std;
int arr[ 5 ] = { 4, 1, 3, 2, 6 }
auto it = arr.find(begin(arr), end(arr), 3)
if(it != end(arr))
cerr << "Found at index: " << (it-begin(arr)) << endl;
else
cerr << "Not found\n";