I want this loop to decrement from the value I set it to from 10 to 0. Why does it keep going forever?
int lengthString = 10;
for (int j = lengthString; lengthString > 0; j--)
{
cout << j;
}
Change to
for (int j = lengthString; j > 0; j--)
lengthString doesn't change in your loop.
Also, you might want to verify that lengthString is greater than 0 (in your real code) if you declare j as an int, because if j is initialized to a negative number, the loop will run "forever" (until it overflows and gets decreased to 0 again, that is).
You're exit condition is lengthString > 0, and since lengthString doesn't change in you're loop you're just saying while(10 > 0), which is true forever.
I believe you meant:
for (int j = lengthString; j > 0; j--)
for (int j = lengthString; j >= 0; j--)
Your loop decrements j, and checks if lengthString is > 0.
int lengthString = 10;
for (int j = lengthString; j > 0; j--)
{
cout << j;
}
You are only decrementing j, but loop condition doesn't use j
It should be j that you are checking in your greater than, not lengthstring.
for (int j = lengthString; j > 0; j--)
Try this one:
for (int lengthString = 10, *j = &lengthString; lengthString > 0; lengthString--)
{
cout << *j;
}
Related
I am creating a sort selecting that puts the max number to the right. I think i have it mostly done and when i execute the code it works but it does not sort the first two numbers in order from smallest to largest.
When I compile I get 2,1,4,7,9,12,99
int n = 7;
int list[] = { 9, 2, 99, 4, 1, 12, 7};
int maxIndex = n - 1;
for (int i = 0; i < maxIndex-1; i++)
{
int maxNum = i;
for (int j = i+1; j < n; j++)
{
if (list[j] > list[maxNum])
{
maxNum = j;
}
int temp = list[j];
list[j] = list[maxNum];
list[maxNum] = temp;
}
}
Im not too sure if its the for function or it has to do with the swap that i have but i dont think its the swap or else the it wouldn't order the other numbers.
So there are a few problems. The first is that your loop limit is incorrect
for (int i = 0; i < maxIndex-1; i++)
should be
for (int i = 0; i < n-1; i++)
You can get rid of the maxIndex variable.
Second your swap is in the wrong place and you swap the wrong elements. You are supposed to swap the maximum value with the current value, so the swap should go after the j loop and not inside it. It's only after the j loop completes that you know what the maximum value is. Plus what you swap is list[i] (the current value) with list[maxNum] (the maximum value). Like this
int maxNum = i;
for (int j = i+1; j < n; j++)
{
if (list[j] > list[maxNum])
{
maxNum = j;
}
}
int temp = list[i];
list[i] = list[maxNum];
list[maxNum] = temp;
Incidentally I don't like the variable name maxNum because it is not a number it's an index. Maybe maxNumIndex would be a better name?
I would like to know why the inner for loop executes. My understanding is that since j = 1 and i = 0, j <= i; would produce nothing as j is already greater than i. Therefore, the inner loop would be skipped, producing the same result as the initial array.
void sortArray(int myArray[], int size)
{
int num1 = 0;
int num2 = 0;
int temp = 0;
for (int i = 0; i < size; i++)
{
int first = 0;
for (int j = 1; j <= i; j++)
{
if (myArray[j] > myArray[first])
{
first = j;
}
temp = myArray[first];
myArray[first] = myArray[i];
myArray[i] = temp;
}
}
}
The inner loop is skipped during first iteration. After i is incremented, j is now equal to i, therefore the inner loop will execute 1 time. Inner loop exits, outer loop increments i by 1, inner loop now iterates twice as i == 2.
This will repeat until i == size - 1 OR i < size (same condition, different wording).
The inner loop should skip for 1 iteration of the outer loop then will run for 1 time as j<=i returns true. (first iteration i=0, second iteration i=1)
this is equivalent to:
for (int j = 1; j <= 0; j++)
After that the second loop will run 1 time equivalent to
for (int j = 1; j <= 1; j++)
and so on..
for (int j = 1; j <= 2; j++)
Let's say I have three vectors.
#include <vector>
vector<long> Alpha;
vector<long> Beta;
vector<long> Gamma;
And let's assume I've filled them up with numbers, and that we know they're all the same length. (and we know that length ahead of time - let's say it's 3.)
What I want to have at the end is the minimum of all sums Alpha[i] + Beta[j] + Gamma[k] such that i, j, and k are all unequal to each other.
The naive approach would look something like this:
#include <climits>
long min = LONG_MAX;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k=0; k < 3; k++) {
if (i != j && i != k && j != k) {
long sum = Alpha[i] + Beta[j] + Gamma[k];
if (sum < min)
min = sum;
}
}
}
}
Frankly, that code doesn't feel right. Is there a faster and/or more elegant way - one that skips the redundant iterations?
The computational complexity of your algorithm is an O(N^3). You can save a very small bit by using:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if ( i == j )
continue;
long sum1 = Alpha[i] + Beta[j];
for (int k=0; k < 3; k++) {
if (i != k && j != k) {
long sum2 = sum1 + Gamma[k];
if (sum2 < min)
min = sum2;
}
}
}
}
However, the complexity of the algorithm is still O(N^3).
Without the if ( i == j ) check, the innermost loop will be executed N^2 times. With that check, you will be able to avoid the innermost loop N times. It will be executed N(N-1) times. The check is almost not worth it .
If you can temporarily modify the input vectors, you can swap the used values with the end of the vectors, and just iterate over the start of the vectors:
for (int i = 0; i < size; i++) {
std::swap(Beta[i],Beta[size-1]); // swap the used index with the last index
std::swap(Gamma[i],Gamma[size-1]);
for (int j = 0; j < size-1; j++) { // don't try the last index
std::swap(Gamma[j],Gamma[size-2]); // swap j with the 2nd-to-last index
for (int k=0; k < size-2; k++) { // don't try the 2 last indices
long sum = Alpha[i] + Beta[j] + Gamma[k];
if (sum < min) {
min = sum;
}
}
std::swap(Gamma[j],Gamma[size-2]); // restore values
}
std::swap(Beta[i],Beta[size-1]); // restore values
std::swap(Gamma[i],Gamma[size-1]);
}
I have to write a code which sort digits in one entered number.
For example: input: 4713239
output: 1233479
It doesn't work properly when I enter repeating digits(like 33) when I have the last loop as FOR:
for(int j = 0; j < arr[i]; j++) // in this loop my output is: 123479.
When I change this loop from FOR to WHILE it works properly.
It means:
while(arr[i]) // and the number is sorted correctly (1233479)
True be told, I don't know what is the difference between these operations in this code.
Why FOR loop doesn't work properly? Could somebody explain me this?
I wrote a code:
int sort(int arg)
{
int var, score = 0;
int arr[10] = {0};
for(int i = 0; i < 10; i++)
{
var = arg % 10;
arr[var]++;
arg = arg / 10;
}
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < arr[i]; j++) //while(arr[i]) --> works correctly
{
score = score * 10 + i;
arr[i]--;
}
}
return score;
}
You modify both arr[i] and j, therefore the loop will end too fast when both are part of the comparison.
for(int j = 0; j < arr[i]; j++) // increase j, compare with arr[i]
{
score = score * 10 + i;
arr[i]--; // decrease arr[i]
}
i'm trying to initialize all cells of a matrix with NULL values, but something is wrong here.
the code :
vector<vector<Distance*> > distanceMatrix;
for (int i = 0; i < 7 ; i++)
for (int j = 0; j < 7 ; j++)
distanceMatrix[i][j].push_back(NULL);
i bet it's something stupid, thanks for the help.
From the std::vector reference page:
Vectors can be constructed with some values in them.
You may try:
vector<vector<Distance*> > distanceMatrix(7, vector<Distance*>(7, NULL));
Also, regarding your problem:
vector<vector<Distance*> > distanceMatrix;
for (int i = 0; i < 7 ; i++)
for (int j = 0; j < 7 ; j++)
distanceMatrix[i][j].push_back(NULL); //1
When you code first reach //1, distanceMatrix[i] resolves to distanceMatrix[0] but you did not call distanceMatrix.push_back(vector<Distance*>()) so you are referring to a non initialized cell.
To correct code would have been:
vector<Distance*> vec;
for (int j = 0; j < 7 ; j++)
vec.push_back(NULL);
vector<vector<Distance*> > distanceMatrix;
for (int i = 0; i < 7 ; i++)
{
distanceMatrix.push_back(vec);
}
Which is still far worse than my first suggestion.
Since the matrix is empty to begin with, you need to push_back every value and every row, not depending on its position in the matrix:
vector<Distance*> row;
for(int j=0; j < 7; j++)
row.push_back(NULL);
vector<vector<Distance*> > distanceMatrix;
for(int i=0; i < 7; i++)
distanceMatrix.push_back(row);