I got an assignment to reverse an dynamic array in C++. So far, from my logic, I thinking of loop thru the array to reverse it. And here comes my code :
int main ()
{
const int size = 10;
int num_array[size];
srand (time(NULL));
for (int count = 0; count< sizeof(num_array)/sizeof(num_array[0]) ; count++){
/* generate secret number between 1 and 100: */
num_array[count] = rand() % 100 + 1;
cout << num_array[count] << " " ;
}
reverse(num_array[size],size);
cout << endl;
system("PAUSE");
return 0;
}
void reverse(int num_array[], int size)
{
for (int count =0; count< sizeof(num_array)/sizeof(num_array[0]); count++){
cout << num_array[sizeof(num_array)/sizeof(num_array[0])-1-count] << " " ;
}
return;
}
Somehow I think my logic was there but this code doesn't works, there's some error. However, my teacher told me that this isn't the way what the question wants. And here is the question :
Write a function reverse that reverses the sequence of elements in an array. For example, if reverse is called with an array containing 1 4 9 16 9 7 4 9 11,
then the array is changed to 11 9 4 7 9 16 9 4 1.
So far, she told us in the reverse method, you need to swap for the array element. So here's my question how to swap array element so that the array entered would be reversed?
Thanks in advance.
Updated portion
int main ()
{
const int size = 10;
int num_array[size];
srand (time(NULL));
for (int count = 0; count< size ; count++){
/* generate secret number between 1 and 100: */
num_array[count] = rand() % 100 + 1;
cout << num_array[count] << " " ;
}
reverse(num_array,size);
cout << endl;
system("PAUSE");
return 0;
}
void reverse(int num_array[], const int& size)
{
for (int count =0; count< size/2; count++){
int first = num_array[0];
int last = num_array[count-1];
int temp = first;
first = last;
last = temp;
}
}
You reverse function should look like this:
void reverse(int* array, const size_t size)
{
for (size_t i = 0; i < size / 2; i++)
{
// Do stuff...
}
}
And call it like:
reverse(num_array, size);
I am no C++ programmer, however I do see an easy solution to this problem. By simply using a for loop and an extra array (of the same size) you should be able to reverse the array with ease.
By using a for loop, starting at the last element of the array, and adding them in sequence to the new array, it should be fairly simple to end up with a reversed array. It would be something like this:
Declare two arrays of the same size (10 it seems)
Array1 contains your random numbers
Array2 is empty, but can consist of 10 elements
Also declare an integer, which will keep track of the progression of the for loop, but in the opposite direction. i.e not from the end but from the start.
Counter = 0
Next you will need to create a for loop to start from the end of the first array, and add the values to the start of the second array. Thus we will create a for loop to do so. The for loop will be something like this:
for(int i = lengthOfArray1; i > 0; i--){
Array2[Counter] = Array1[i]
Counter++
}
If you only wish to print it out, you would not need the counter, or the second array, you will simply use the Array1 elements and print them out with that style of for loop.
That's it. You could set Array1 = Array2 afterward if you wished to keep Array1 the original for some reason. Hope this helps a bit, changing it to C++ is your job on this one unfortunately.
You're not actually swapping the elements in the array, you're just printing them out. I assume she wants you to actually change what is stored in the array.
As a hint, go through the array swapping the first and last element, then the 2nd and 2nd last element, etc. You only need to loop for size/2 too. As you have the size variable, just use that instead of all the sizeof stuff you're doing.
I would implement the function like following
void reverse(int A[], int N)
{
for (int i=0, j=N-1; i<j; i++, j--){
int t = A[i];
A[i] = A[j];
A[j] = t;
}
}
Related
EDIT: I've solved the issue! It had nothing to do with the function itself. I initially used a vector for this function, and the cout statement I used to check the function in main() still called the vector, not the array. A simple mistake, but I appreciate the help!
Hey! I'm currently in college learning data structures, and for our final project, we're tasked with creating multiple sorting algorithms to sort 500,000 randomly generated numbers between 1 - 9,999,999 that have been defined in a text file. I'm currently trying to work on a counting sort, and I keep getting the #include vector line 1553 error 'vector subscript out of range'. I've debugged this function all the way to the last for-loop. Everything seems to work perfectly fine, so I'm assuming it has to do with somewhere in this final piece, but I'd prefer to not have to manually go through 500,000 cycles, so if anyone can see what I've done wrong, I'd like to know.
I also allocated this data on the heap because stack allocation creates a memory overload.
I apologize if this is a low-level question, but I'd love some help, as this project means a lot to me and to my grade. Thank you!
void countingSort(int numberArray[], int SIZE)
{
// Initializer for dynamically-allocated array used to hold the sorted data in the array
int* sortedArray = new int[SIZE];
// Initializes all values in sortedArray to 0
for (int i = 0; i < SIZE; i++)
sortedArray[i] = 0;
// Initializer for variable used to hold the maximum value in the original data
int max = 0;
// Finds the max in numberArray
for (int i = 0; i < SIZE; i++)
{
if (numberArray[i] > max)
max = numberArray[i];
}
// Create an array to store the amount of times each number in numberArray is used
int* countArray = new int[max + 1];
// Initialize all indexes of countArray to 0
for (int i = 0; i <= max; i++)
{
countArray[i] = 0;
}
// When a number is present in numberArray, increase its number of appearances in countArray by one
for (int i = 1; i < SIZE; i++)
countArray[numberArray[i]]++;
// Find the total frequency in the count array
for (int i = 1; i <= max; i++)
countArray[i] += countArray[i - 1];
// Store the sorted values into a sorted array
// Decrease the total count number
for (int i = SIZE - 1; i > 0; i--)
{
sortedArray[countArray[numberArray[i]] - 1] = numberArray[i];
countArray[numberArray[i]]--;
}
// Store the sorted array in the original numberArray
for (int i = 0; i < SIZE; i++)
{
numberArray[i] = sortedArray[i];
}
}
int main()
{
int* SIZE = new int;
*SIZE = 500000;
.
(*code for other functions*)
.
countingSort(numberArray, *SIZE);
cout << "\n" << numberList[0] << "\t" << numberList[499999] << endl;
}
Number can't be in array if it can be divided by number of elements of array (for example: in array which has 10 elements, numbers 1,2,5 and 10 are not "welcome"). So I need to find all these elements in array and kick them out. After that length of array changes, and then some other elements can be "not welcome" in array. I have to repeat it until array is without these elements. In the end, I have to calculate remaining elements and print them out. (I'm using C++)
I didn't know how to delete element from array, and just set value to 0.
I get input n (number of elements in array) and then all of these elements.
So, I already tried it but I'm sure there is much more effective way to do it :P Here is the code:
int main()
{
short int b = 0;
short int n;
int result = 0;
cin >> n;
int m = n;
int numbers[n];
for (int i = 0; i < n; i++) {
cin >> numbers[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j<=n; j++) {
if(numbers[j] != 0) {
if(n % numbers[j] == 0) {
numbers[j] = 0;
b = b + 1;
} }
}
n = n - b;
b = 0;
}
for (int i = 0; i < m; i++) {
result += numbers[i];
}
cout << result;
return 0;
}
example input: 10 1 2 3 4 5 6 7 8 9 10
example output: 24
I didn't know how to delete element from array
It is not possible to "delete element from array". An array of n elements begins its life with n elements, has n elements throughout its entire lifetime, and ends its life with n elements. It is not possible to change the size of an array.
Another problem:
cin >> n;
int numbers[n];
The size of an array must be a compile time constant. n is not a compile time constant. This is not a well-formed C++ program.
An array of runtime size must be allocated dynamically. The easiest solution is to use std::vector. The size of a vector can change, and you can use std::vector::erase to remove elements from it.
So I was asked to write a program which uses a pointer that points to the first element in an array and pass the pointer to a function. Then using only pointer variables (and looping constructs), print only the array values that are exact multiples of 7. Here's the script:
#include <iostream>
using namespace std;
void print_sevens(int *nums,int length){
for(int i = 0; i < length; i++){
nums = nums + i;
if(*nums % 7 == 0)
cout << *nums << endl;
}
}
int main() {
int a[5]={7,49,2,8,70};
int *p1 = &a[0];
print_sevens(p1,5);
}
The output from this is :
7
49
-149462114
I can't find out what is wrong. Any help is appreciated. Thanks
nums is the pointer to the start of the array. You are reassigning it at every loop iteration to be nums + i, not nums + 1. So, at the fourth iteration, for example, nums points to the initial array start + 0 + 1 + 2 + 3, which is the seventh element in your array of 5 elements. That's why you get garbage.
Use a subscript to make your life easy:
for(int i = 0; i < length; i++){
if(nums[i] % 7 == 0)
cout << nums[i] << endl;
}
For my homework it is given one dimensional array and i have to convert it in a two dimensional array. The two dimensional array has 2 for the number of columns, because i have to represent the one dimensional array as pairs(the value of the number, the number of appearences in the array).
This is what have tried. The error appears on the last 2 lines of code: access violation writing location 0xfdfdfdfd.
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
const int NR=17;
int arr[NR]={6,7,3,1,3,2,4,4,7,5,1,1,5,6,6,4,5};
int **newArr;
int count=0;
int countLines=0;
int searched;
for(int i=0;i<NR;i++)
{
newArr=new int*[countLines];
for(int i=0;i<countLines;i++)
{
newArr[i]=new int[2];
}
searched=arr[i];
if(i>0)
{
for(int k=0;k<countLines;k++)
{
if(newArr[countLines][0] == searched)
{
searched=arr[i]++;
}
for(int j=0;j<NR;j++)
{
if(searched==arr[j])
{
count++;
}
}
countLines++;
}
}
else
{
for(int j=0;j<NR;j++)
{
if(searched==arr[j])
{
count++;
}
}
countLines++;
}
newArr[countLines][0]=searched;
newArr[countLines][1]=count;
}
}
First you are using newArr in the first loop before allocating it any memory. You cannot dereference a pointer which owns no legal memory. It results in undefined behavior.
Secondly in the last part, you are allocating newArr a memory equal to countLines thus.
newArr = new int*[countLines] ;
It means that the indices in the first dimension of newArr are 0------>countLines-1. Doing newArr[countLines][0] = searched ; is again undefined. Make it newArr[countLines - 1].
I'm not going to bother with a line-by-line code analysis since (a) you're changing it while people are answering your question and (b) it would literally take too long. But here's a summary (non-exhaustive) of klunkers:
You are leaking memory (newArr) on each loop iteration starting with the second.
You're out-of-bounds on your array access multiple times.
You should not need to use a pointer array at all to solve this. A single array of dimension [N][2] where N is the number of unique values.
One (of countless many) way you can solve this problem is presented below:
#include <iostream>
#include <algorithm>
int main()
{
// 0. Declare array and length
int arr[]={6,7,3,1,3,2,4,4,7,5,1,1,5,6,6,4,5};
const size_t NR = sizeof(arr)/sizeof(arr[0]);
// 1. sort the input array
std::sort(arr, arr+NR);
/* alternaive sort. for this input size bubble-sort is
more than adequate, in case your limited to not being
allowed to use the standard library sort */
/*
for (size_t i=0;i<NR;++i)
for (size_t j=i+1;j<NR;++j)
if (arr[i] > arr[j])
{
arr[i] ^= arr[j];
arr[j] ^= arr[i];
arr[i] ^= arr[j];
}
*/
// 2. single scan to determine distinct values
size_t unique = 1;
for (size_t i=1;i<NR;++i)
if (arr[i] != arr[i-1])
unique++;
// 3. Allocate a [unique][2] array
int (*newArr)[2] = new int[unique][2];
// 4. Walk array once more, accumulating counts
size_t j=0;
newArr[j][0] = arr[0];
newArr[j][1] = 1;
for (size_t i=1;i<NR;++i)
{
if (arr[i] != arr[i-1])
{
newArr[++j][0] = arr[i];
newArr[j][1] = 0;
}
++newArr[j][1];
}
// 5. Dump output
for (size_t i=0;i<unique;++i)
cout << newArr[i][0] << " : " << newArr[i][1] << endl;
delete [] newArr;
return EXIT_SUCCESS;
}
Output
1 : 3
2 : 1
3 : 2
4 : 3
5 : 3
6 : 3
7 : 2
I'm new to C++ and I'm trying to do one thing that is easy in python using slice lists,
but I can't find a easy way to do that in c++.
I need to reorder a array to start at a given element like:
int array[] = {1,2,3,4,5};
reordered array to start at element 3:
{3,4,5,1,2}
this is the way I found to do that, but it seems to be a bit overkill:
void Graph::reorder(int x, MIntArray ¤tArray)
{
MIntArray reorderedIndices;
int index;
for (unsigned int i=0; i<currentArray.length();i++){if(currentArray[i]==x){index=i;}} // get the index
for (unsigned int i=index; i<currentArray.length();i++){reorderedIndices.append(currentArray[i]);} // zero to index
for (unsigned int i=0; i<index;i++){reorderedIndices.append(currentArray[i]);} // index to last
for (unsigned int i=0; i<currentArray.length();i++){currentArray.set(reorderedIndices[i],i);} // transfer
}
any help would be much appreciated!!
thanks
luiz
Use std::rotate method to do this reordering. Supply the beginning of the array as the first parameter, the end of the array (i.e. array+length) as the last parameter, and the "midpoint" as the second parameter. Midpoint defines the index of the element to be moved to the initial position.
int x[] = {1,2,3,4,5};
rotate(x, x+2, x+5);
for (int i = 0 ; i != 5 ; i++)
cout << x[i] << " ";
cout << endl;
This prints 3 4 5 1 2