So I got this code sample that needs fixing. From what I can gather it takes an array, reverses it and then counts all the elements within it. Here is the code.
//-------------------------------------------------------------------
void ReverseTheArray( const short *pArrayStart, const int nArrayByteLength )
{
const short *pArrayEnd = (pArrayStart + nArrayByteLength);
while(pArrayStart != pArrayEnd)
{
short tmp = *pArrayStart;
*pArrayStart = *pArrayEnd;
*pArrayEnd = tmp;
pArrayStart++;
pArrayEnd--;
}
}
//-------------------------------------------------------------------
int CountTheArrayContents( const short *pArrayStart, int nNumEntries )
{
assert(nNumEntries-- > 0);
int nCount = 0;
for(unsigned uArrayIndex = nNumEntries; uArrayIndex >= 0; uArrayIndex--)
{
nCount += pArrayStart[uArrayIndex];
}
return nCount;
}
const short g_nSomeNumbers[] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
};
//-------------------------------------------------------------------
int main()
{
short *pDataArray = new short[10];
memcpy(pDataArray, g_nSomeNumbers, sizeof(g_nSomeNumbers));
ReverseTheArray(pDataArray, 10*sizeof(short));
int nCount = CountTheArrayContents(pDataArray, 10);
printf("Sum total is %0.02f\n", nCount);
return 0;
}
I have ideas of what the problems are but can't seem to figure out a simple solution to the problem, one that doesn't require rewriting the entire code. If anyone can read this and see how the errors can be fixed it would be much appreciated.
I'm going to mention some things that I think are causing problems.
All the parameters and the pArrayEnd variable in the ReversTheArray() function are all const but are trying to be changed within the while loop, which is throwing an error. Should the const's be removed? Is there a reason const's would be used for this?
If the const's are remove a runtime error happens when trying to run the for loop in the CountTheArrayContents() function expressing an unhandled exception and saying "Access violation reading location 0x003DFFFE". Drawing a complete blank on that one.
Again any help on the code would be very much appreciated and I couldn't thank you guys enough.
PS. This is a challenge to create a reverse and accumulate function so I'm looking for a fix for the code and not a removal of the two functions. Thank you
PSS. Thanks to everyone who answered. I'm glad I did this (this being the first problem that I've posted about myself) and you've all been a huge help. I've got to say I've learnt alot.
Adding the actual length in bytes will add too many because pointer arithmetic is defined in terms of units of the size of the type pointed to. That is, pArrayEnd becomes &pDataArray[10 * sizeof(short)] instead of &pDataArray[10]. You don't need to multiply by sizeof(short) when calling the reversal function. Alternatively, you can divide nArrayByteLength by sizeof(short) when calculating the initial value of pArrayEnd.
The second issue is the fact that you only have 10 elements (0..9) allocated, meaning &pDataArray[10] would be one element beyond the array. The reversal function then tries to assign data to this unallocated area of memory, which can cause problems. The function should initialize pArrayEnd as shown, but immediately after, it should decrement pArrayEnd by 1. This way you won't be assigning to memory that might not belong to you. Beware of pArrayStart == pArrayEnd before you decrement pArrayEnd. An alternative test would be to ensure nArrayByteLength != 0.
Another problem is if the array has an even number of elements, and you try to do a reversal. If it does have an even number (like 10), pArrayStart will point to pDataArray[4], pArrayEnd will point to pDataArray[5], and after the data is assigned, pArrayStart++ will make pArrayStart point to pDataArray[5] and pArrayEnd-- point to pDataArray[4]. Then (6,3), (7,2), (8,1), (9,0), ... In other words, pArrayStart will never be equal to pArrayEnd in such a case. Instead, you should ensure that pArrayStart < pArrayEnd.
Hope this helps!
Also, any reason for not using std:: reverse? Just wondering.
Edit
The accumulation function can be rewritten as the following, which will avoid the issue with the assert macro while doing the same thing:
int CountTheArrayContents( const short *pArrayStart, int nNumEntries )
{
int count = 0;
assert(nNumEntries);
while (nNumEntries--)
count += pArrayStart[nNumEntries];
return count;
}
Hopefully count doesn't overflow.
If all you're trying to do is reverse the contents of the array and accumulate the result, std::reverse and std::accumulate will do the trick (per the suggestion by #chris). Here's an example, which maintains the dynamically allocated short*. A better solution would use std::vector or std::array.
#include <algorithm>
#include <numeric>
#include <stdio.h>
#include <memory.h>
const short g_nSomeNumbers[] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
};
int main()
{
short *pDataArray = new short[10];
memcpy(pDataArray, g_nSomeNumbers, sizeof(g_nSomeNumbers));
std::reverse(pDataArray, pDataArray+10);
int nCount = std::accumulate(pDataArray, pDataArray+10, 0);
for( size_t i=0; i<10; ++i )
printf("%d ", pDataArray[i]);
printf("\n");
printf("Sum total is %d\n", nCount);
delete [] pDataArray;
return 0;
}
This prints
9 8 7 6 5 4 3 2 1 0
Sum total is 45
Related
Can someone help me with the code in c++ below?
#include <iostream>
#include <fstream>
using namespace std;
int PARENT(int i)
{
return (i/2);
}
int Heapify_up(int arra[], int i)
{
int j,k;
if (i>1){
j = PARENT(i);
if (arra[i]<arra[j]){
k=arra[i];
arra[i]=arra[j];
arra[j]=k;
Heapify_up(arra, j);
}
}
return j;
}
int main()
{
int array3[15];
int i,p,array_length;
ifstream inputFile1("Heapfile.txt");
if (inputFile1.good()){
int current_number = 0;
i=1;
while (inputFile1>> current_number)
array3[i++] = current_number;
inputFile1.close();
}
array_length = i;
cout<<"Please, enter an integer: ";
cin>>p;
array3[array_length+1]=p;
int pos=Heapify_up(array3, array_length+1);
for (i=1; i<15; i++){
cout<<array3[i]<<" ";
}
cout<<"The position is "<<pos;
}
Let me explain you that have an array in a txt file. After i insert a random integer and with the heapify-up algorith I'm sorting this random number to the array. I want to print the new sorted array(I' have done that) and the new position of the random element that i have entered. Any idea?
thanks in advance!
P.S. I am new here and i find it somehow difficult to post my code correctly... still learning! XD
Okay, there are multiple problems with your code.
You make no effort to ensure you don't blow past the size of your
static array.
You skip a spot in the array when appending your manually-added
value
Your array length is wrong
You aren't initializing your variables
Let's start with the last one. Please do something like this:
int i{0}, p, array_length;
This ensures the i variable is properly initialized to zero.
Next, your code does this:
array3[i++] = current_number;
This means that at any given time, i is the length of the array.
But later you do this:
array_length = i;
array3[array_length+1]=p;
Frankly, I would drop variable i entirely and use array_length instead. There is no need for both.
But even without that, you're setting array_length correctly, but then you're inserting to a point AFTER that, so you might start with:
[ 1, 2, 3, 4, 5 ]
At this point, i == 5. Input a 6 and have:
[ 1, 2, 3, 4, 5, 0, 6 ]
Because you put it at index i+1 not at index i.
At this point, array_length is no longer an accurate length. But you do this:
int pos=Heapify_up(array3, array_length+1);
So it kind of works.
I don't know why Heapify_up is returning j -- it's just the midpoint of the array. That's not a useful value.
Furthermore, I don't really know what your heapify thing is trying to accomplish.. It certainly isn't a heap-sort. If the middle and end numbers are in sorted order, it doesn't actually do a thing.
This URL might help you with some code:
Heap sort at Geeks for Geeks
As for using a fix-length array -- that's problematic, too, but using std::vector is probably a bit much for you. I'd make sure that your input loop doesn't run into issues or start with a much longer beginning array.
I am supposed to make a function that accepts an integer array as an input, and then randomly modify each value within the array by either -38 or 55, specifically. However, I'm not sure how to randomly choose whether a number will get deducted by -38 or added by 55, and how to go about this process.
I tried making an if statement within my function, stating that if a function is even (array[i] % 2), then deduct by -38, and if the number is odd (else statement), add by 55. While this may work, it's not the right way to answer this question, because what if I wanted to add an even number by 55? Or vice versa with an odd number with subtracting 38?
#include <iostream>
using namespace std;
#include <time.h>
#include <cstdlib>
int output(int array[]){
for(int i = 0; i<5; i++){
cout << array[i] << " ";
}
}
int offset(int array[]){
for (int j = 0; j < 5; j++) {
int myrand = rand();
if (myrand % 2 == 0){
array[j] = array[j] - 38;
}
else{
array[j] = array[j] + 55;
}
}
}
int main()
{
srand(time(NULL));
int array[5] = {1, 2, 3, 4, 5};
cout << "Original: ";
output(array);
offset(array);
return 0;
}
If the value within an array is 5, it should have the possibility of being 10 or 0, and so forth with any numbers.
Let's get you started and then you can go from there. When beginning to learn any programming language, (or any language for that matter), you start with a large amount of what makes up the language, what are the words and what are the rules for putting them together. For a programming language that is its "syntax". Every language has its own. You begin learning the syntax with minimal examples that make use of the basic types and progress from there.
With a compiled language, you add to that, how you translate the collection of proper syntax in a file into a machine readable executable. How you compile the source code into an executable.
At this point, the most important thing you can do is make friends with your compiler and listen to what it is telling you. You do this most effectively by enabling compiler warnings so your compiler will point out for you the line (and in many cases the character in the line) that it finds troubling. The rule is
"Always compile with warnings enabled, and do not accept code
until it compiles cleanly without warning."
To enable warnings add -Wall -Wextra -pedantic to your gcc/clang compile string. For VS (cl.exe on windows), use /W3. There are always additional warnings you can add, but this will provide a thorough set that if you follow the rule will save you hours of time debugging.
With that as background, let's look at what you are trying to do. First, you have tagged your Question as C++. I provided you in the comment above a place to start to determine what basic header files were needed in your proposed code. C++ Standard Library headers. Checking you will find that C++ provides an implementation of time.h as the header ctime (all of the standard C headers are generally named c......). So here, based on what you included in your code, you would need, at minimum,
#include <iostream>
#include <cstdlib>
#include <ctime>
While you can use 5 in your code, you want to avoid hard-coding numbers (unless required by the standard library function being used, such as the C scanf field-width modifier, which does not allow a named constant or variable in that case). Hard-coding numbers is called using magic-numbers and is best avoided.
When you declare and initialize an array, you can declared the array with an empty [] (which would normally be an incomplete type), but by virtue of providing a braced-initializer, the compiler uses the number of elements provided to size the array at compile time. You can then determine the number of elements with the array by dividing the sizeof array by the sizeof (an_element). You can simply derefernce the array to obtain the first element, so you will see it written as follows:
int main (void) {
int array[] = {1, 2, 3, 4, 5};
int nelem = sizeof array / sizeof *array; /* number of elements */
...
Now when passing basic-type arrays to a function, you also need to pass the number of elements as well. Why? When an array is passed as a parameter (actually on any access, subject to limited exceptions), an array is converted to a pointer to the first element. If you tried to use the sizeof array / sizeof (an_element) within a function after passing the array as a parameter, you would end up with sizeof (a_pointer) / sizeof (an_element) which is certainly not going to work.
So for example, in your output() function, to pass the number of elements along with the array, you would need:
void output (int array[], int nelem)
{
for (int i = 0; i < nelem; i++) {
cout << array[i] << " ";
}
cout << '\n';
}
The same would apply equally to your offset() function:
void offset (int array[], int nelem)
{
for (int j = 0; j < nelem; j++) {
int myrand = rand();
if (myrand % 2 == 0) {
array[j] = array[j] - 38;
}
else{
array[j] = array[j] + 55;
}
}
}
(note: the return type for each function has been changed to void rather than int. Neither function need provide an indication of success/failure and neither is returning a value that is being used back in the calling function, main() here. In each case you are simply printing the elements of the array, or updating their values in-place.)
The remainder for your first attempt is simply to output the array before applying the offset, and then again afterwards. Your main() could be:
int main (void) {
int array[] = {1, 2, 3, 4, 5};
int nelem = sizeof array / sizeof *array; /* number of elements */
srand (time(NULL)); /* seed random number generator */
cout << "Original: "; /* output original array */
output (array, nelem);
offset (array, nelem); /* apply offset to array */
cout << "Updated : "; /* output original array */
output (array, nelem);
return 0;
}
If you put all the pieces together, and compile with warnings enabled, will find your code compiles without warning and produces a working executable.
Example Use/Output
$ ./bin/randaddtoarray
Original: 1 2 3 4 5
Updated : 56 57 58 -34 -33
If you run it multiple times you will probably see that you don't always have an even number of odd or even random numbers. That is the nature of random numbers.
As mentioned in the comments, there are many many ways to approach developing a random scheme for applying your -38 or +55. Take some time and research the different methods and try implementing them and see how your results change. You will probably want to add more than 5 values to your array to be able to draw any kind of distribution conclusion. Five values is really not enough to provide any type of clear comparison.
Let me know if you have further problem.
Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having an non-negative integer size. Since he likes to make things perfect, he wants to arrange them from smallest to largest so that each statue will be bigger than the previous one exactly by 1. He may need some additional statues to be able to accomplish that. Help him figure out the minimum number of additional statues needed.
Example
For statues = [6, 2, 3, 8], the output should be
makeArrayConsecutive2(statues) = 3.
This is a problem from codefights.
Here is my code written below
#include <iostream>
#include <vector>
#include<algorithm>
using std::vector;
int makeArrayConsecutive2(std::vector <int> statues) {
vector<int>::size_type size = statues.size();
sort( statues.begin(), statues.end() );
int counter = 0;
for( int i = 0; i<size; i++ )
{
int dif = statues[i+1] - statues[i] - 1;
if( dif >= 1 ) {counter+=dif;}
}
return counter;
}
int main()
{
vector<int> c = {1,2,3,4,5,6,7,8,9,10};
std :: cout<<"You need "<<makeArrayConsecutive2(c)<<" statues"<<std::endl;
return 0;
}
When I run code with this certain value of vector c it outputs misunderstanding value.All other cases runs correct, but when I declare 10 dimensional vector(I mean vector with 10 values) it doesn't work correct .Could you please explain what's the problem?
On the last iteration of for cycle statues[i+1] will be out of bounds causing undefined behavior. You need to add checks that statuses is not empty before for loop and then iterate until size - 1.
Here is the chunk of code in question that I've pulled from my program:
#include <vector>
using namespace std;
vector<double> permittingConstructionCosts(56);
static const int PERMITTING_PERIODS = 0;
static const int CONSTRUCTION_PERIODS = 11;
static const double CONSTRUCTION_COSTS = 2169506;
static const double PERMITTING_COSTS = 142085;
static const int PERMITTING_CONSTRUCTION_PERIODS = PERMITTING_PERIODS + CONSTRUCTION_PERIODS;
void calcExpenses // Calculates permitting and construction expenses
(
vector<double>& expense,
double value1,
double value2
)
{
int i;
for (i=0; i<=PERMITTING_PERIODS + 1; i++)
{
expense[i] = value1;
}
for (i=PERMITTING_PERIODS + 2; i<expense.size(); i++)
{
if (i < PERMITTING_CONSTRUCTION_PERIODS + 2)
{
expense[i] = value2;
}
}
}
int main()
{
if (PERMITTING_PERIODS != 0)
{
calcExpenses(permittingConstructionCosts, -PERMITTING_COSTS/PERMITTING_PERIODS, -CONSTRUCTION_COSTS/CONSTRUCTION_PERIODS);
}
else
{
calcExpenses(permittingConstructionCosts, 0, -CONSTRUCTION_COSTS/CONSTRUCTION_PERIODS);
}
return 0;
}
According to ideone (http://ideone.com/LpzUny) the code has a runtime error that returns "time: 0 memory: 3456 signal:11".
I've tried to look for solutions on SO and found the following links:
How can I avoid a warning about division-by-zero in this template code?
How to eliminate "divide by 0" error in template code
However, I don't know how to use templates because I am new to c++ and I'm not sure I need to use them in this case so I have no clue how to adapt those solutions to my particular problem if it's even possible.
I'm pretty sure that the "-PERMITTING_COSTS/PERMITTING_PERIODS" is causing the problem but I thought that simply checking the divisor would solve the problem. This function seems to work for every other value other than 0 but I need to account for the case where PERMITTING_PERIODS = 0 somehow.
I would very much appreciate any help I can get. Thanks in advance!
Edit: I actually do initialize the vector in my program but I forgot to put that in because the size is decided elsewhere in the program. The chunk of code works once I fix that part by putting in a number but my program still has a runtime error when I set PERMITTING_PERIODS to 0 so I guess I have to go bug hunting elsewhere. Thanks for the help!
The problem lies inside the function, which is called by the else statement in the main function:
for (i=0; i<=PERMITTING_PERIODS + 1; i++)
{
expense[i] = value1;
}
Here, PERMITTING_PERIODS is 0, thus you loop from 0 to 2 (inclusive).
However, expense.size() is 0, since your vector is empty. As a result, you are trying to access an empty vector, which causes a segmentation fault.
With that said, print the value of i inside the loop, you should see that you try to access expense[0], but the vector is empty, so it has no first slot (basically it doesn't have any)!!
So replace that with:
expense.push_back(value1);
which will allocate enough space for your values to be pushed into the vector.
The answer given in the cited links, (i.e. "How to eliminate "divide by 0" error in template code") applies equally well here. The other answers were given in the context of templates, but this is completely irrelevant. The sample principle applies equally well with non-template code, too. The key principle is to compute a division, but if the denominator is zero, you want to compute the value of zero instead of the division.
So we want to compute -PERMITTING_COSTS/PERMITTING_PERIODS, but use the value of 0 instead of the division when PERMITTING_PERIODS is 0. Fine:
int main()
{
calcExpenses(permittingConstructionCosts,
(PERMITTING_PERIODS == 0 ? 0: -PERMITTING_COSTS)/
(PERMITTING_PERIODS == 0 ? 1: PERMITTING_PERIODS),
-CONSTRUCTION_COSTS/CONSTRUCTION_PERIODS);
return 0;
}
I'm working on a homework assignment for Comp Sci I, and I've tried literally everything I could find on the internet and in my textbook in an attempt to count the number of matching elements in two C++ arrays. I simply can't find a way to make my code count the number of matching elements in two arrays of the same size (same value, same position). For instance, if array1 has 5, 4, 3, 2, 1, and array2 has 1, 2, 3, 4, 5, there is one matching element, the 3. How can I get my code to perform this seemingly simple task?
Couldn't find an answer anywhere. My textbook only shows how to see if the arrays match exactly without counting the number of matches, and I haven't been able to find anyone else with this exact question for C++.
Here's my function so far:
int matchCounter(int lottery[5], int user[5], int matches, int SIZE)
{
int count = 0;
for (count < SIZE; count++;)
{
if (lottery[count] == user[count])
{
matches++;
}
}
return matches;
} // end matchCounter
SIZE is a constant that equals 5, by the way. It would seem that this should work, but every time I run the program, it displays 0 matches, even in there are some matches. If anyone could help me out, I would be forever in your debt. Seriously, there's got to be some incredibly simple thing I'm just not seeing. Thanks for you help in advance.
Your for statement is garbled. Try this:
int matchCounter(int lottery[5], int user[5], int matches, int SIZE)
{
for (int count = 0; count < SIZE; count++)
{
...
For reason why your program always fails: count++ is put in the position of the condition in the for loop, so every time this for loop is executed, count is evaluated and considered to be the condition, and then count is incremented by 1. However, every time count starts from 0 (as in int count = 0;), so the for loop actually never gets executed, and matches remain zero.
EDIT:
From your source code, you're not using your return value, which is also suggested by #jimhark. Change the function signature to:
int matchCounter(int lottery[5], int user[5], int SIZE)
And call with:
matches = matchCounter(lottery, user, 5);
Otherwise, your return value of the function matchCounter is ignored, while int matches only plays the role of a local variable in the function.
Read this about why the matches doesn't work: http://www.cs.utsa.edu/~wagner/CS2213/swap/swap.html (in a similar setting)
Since this is a homework, I will try to point the errors without giving the solution.
The form of a for loop is:
for (INIT; COND; STEP) {
BODY;
}
which is equivalent to:
INIT;
while (COND) {
BODY;
STEP;
}
The way you did, your INIT is count < SIZE, which doesn't do anything. Your "COND" is count++, which returns 0 on the first iteration. As you may know, 0 is false, in the context of a boolean. Therefore, your for loop body is not being executed at all.
Also, you should not pass matches as an argument. It should be a local variable initialized with 0.
Your may not be using your return value. Change sig to:
int matchCounter(int lottery[5], int user[5], int SIZE)
{
matches = 0;
And make sure your're calling with:
matches = matchCounter(lottery, user, 5);
If you need a running sum, use:
matches += matchCounter(lottery, user, 5);
In any case matchCounter doesn't care what running total is, so why pass it in?
Also you may need to sort the lists first.
Maybe this can help:
int matches=0;
for(int i=0;i<ARRAY.SIZE;i++) {
for(int j=0;j<ARRAY.SIZE;j++) {
if(arr1[i]==arr2[j]) {
matches++;
arr2[j]=NULL;
}
}
}