C++ search through 2d array [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I've got a 2d array airport[30][5] which prints out like this:
Departure, Destination, Airline, Price, Duration
I want the user to enter in their own departure and destination (stored in variables x and y), and then I will search through the array to see if the first two elements of each row matches what the user has input. If they both match, I want to print out all the rows where they both match.
So far I have a simple loop but it prints out all the lines in the array:
for(int i = 0; i < 5; i++) {
for(int j=0; j < 30; j++) {
if(airport[i][0] == x && airport[i][1] == y) {
cout << Line(s) from array
}
}
}

something like this?
for(int j=0; j < 30; j++) {
if(airport[j][0] == x && airport[j][1] == y) {
for (int i=0; i < 5; i++) {
cout << airport[j][i] << " ";
}
cout << endl;
}
}

You need only one loop
for ( int i = 0; i < 30; i++) {
if ( airport[i][0] == x && airport[i][1] == y) {
// cout << Current Line from array
}
}

Related

How can I clear the variable after while loop is done [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm writing a program that multiplies matrices. And here I have got two variables "i" and "q" which at the beginning are both 0. While the loops proceed variables ("i" and "q") change their values. However after the loops are done I need "i" and "q" to come back to the value 0, so the loops can repeat themselves for different "w" and "k" . How can I do so??
int wynik[x][z]; //table that holds the result of the multiplication
int i=0;
int q=0;
int wyn=0;
for(int w=0; w<x; w++)
{
for(int k=0; k<z; k++)
{
while((i<y) && (q<v) )
{
wyn = (tab1[w][i] * tab2[q][k]) + wyn;
i++;
q++;
}
wynik[w][k] = wyn;
}
}
set those to 0 after the outer loop is done:
int wynik[x][z]; //table that holds the result of the multiplication
int i=0;
int q=0;
int wyn=0;
for(int w=0; w<x; w++)
{
for(int k=0; k<z; k++)
{
while((i<y) && (q<v) )
{
wyn = (tab1[w][i] * tab2[q][k]) + wyn;
i++;
q++;
}
wynik[w][k] = wyn;
}
//HERE
i = 0;
q = 0;
}
I feel like the most natural way according to your current design would be to change the most inner while loop into for (int i = 0, q = 0; (i < y) && (q < v); i++, q++). This lets for loop manage modification of i and q and both are inside the scope of the inner for loop since they are not needed anywhere else.
I would rethink the design. It sounds like a great example to study some algorithms.
Just dropping this here ;)
https://en.wikipedia.org/wiki/Matrix_multiplication_algorithm

SelectionSorting in c++ (Error : EXC_BAD_ACCESS) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I just recently learned to do selection sorting an array. I am also using X-Code on a mac.
I thought I did everything correctly, but I seem to keep getting this error message on the if statement :
Thread 1: EXC_BAD_ACCESS(code=1, address=0x7fff5fc00000).
What am I doing wrong?
using namespace std;
void selectionSorting(int array[], int n)
{
for(int i = 0; i < n-1; n++)
{
int min = i;
for(int j = i + 1; j < n; j++)
{
if(array[j] < array[min]) //Thread 1: EXC_BAD_ACCESS(code=1, address=0x7fff5fc00000)
min = j;
}
int temp = array[i];
array[i] = array[min];
array[min] = temp;
}
}
int main()
{
int n = 10;
int array[]= {10,9,8,7,6,5,4,3,2,1};
selectionSorting(array, n);
for(int x=0; x < n; x++)
{
cout << array[x] << " ";
}
return 0;
}
You have a logical error at for(int i = 0; i < n-1; n++). It should be for(int i = 0; i < n-1; i++) (iterate through the elements of the array).
Also EXC_BAD_ACCESS suggests that your are trying to access a piece of memory that is no longer accessible or it doesn't go well with the intended use.
See that this occurs at if(array[j] < array[min]), which is obvious because j is going beyond the array length as you do n++.
As suggested in the comments try using a debugger.

Creating Power set from a given vector<string> with error:"Vector subscript out of range" [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I tried the following algorithm to find the power set from a given vector
std::vector<std::string> *NFAStates
std::vector<std::string> *PowerSet
int state_lines = (*NFAStates).size()
for (int i = 1; i < pow(2, state_lines); i++)
{
for (int j = 0; j < state_lines; j++)
{
if (i &(1 << j) && !(*PowerSet)[i].empty())
{
(*PowerSet)[i].append("_");
(*PowerSet)[i].append((*NFAStates)[j]);
}
else if (i &(1 << j) && (*PowerSet)[i].empty())
(*PowerSet).push_back((*NFAStates)[j]);
}
std::cout << (*PowerSet)[i] << std::endl;
}
return 0;
i get an error "Vector subscript out of range"
Here *NFAStates consist of
S0
S1
S2
and *PowerSet is where my powerset elements are stored
it would be very helpful if i know why i am getting this error
I have tested your code, with minimal mods, namely I changed pointers to references. I found no fault with it.
The problem lies elsewhere, probably in your initialization of the input vectors.
#include <string>
#include <vector>
int func(std::vector<std::string>& NFAStates, std::vector<std::string> &PowerSet)
{
int state_lines = NFAStates.size();
//for (int i = 1; i < pow(2, state_lines); i++)
for (int i = 1; i < (1 << state_lines); i++) // this is more effective for powers of 2
{
for (int j = 0; j < state_lines; j++)
{
if (i &(1 << j) && !PowerSet[i].empty()) // <-- can't access [i] if PowerSet is empty.
{
PowerSet[i].append("_");
PowerSet[i].append(NFAStates[j]);
}
else if (i &(1 << j) && PowerSet[i].empty())
PowerSet.push_back(NFAStates[j]);
}
std::cout << PowerSet[i] << std::endl; // <-- can't access [i] if PowerSet is empty.
}
return 0;
}
int main()
{
std::vector<std::string> NFAStates{ "S0", "S1", "S2" };
std::vector<std::string> PowerSet{"0", "1", "2", "3", "4", "5", "6", "7" };
return func(NFAStates, PowerSet);
}
The program, notably, bugs when PowerSet is initially empty.
Maybe adding PowerSet.resize(1 << NFAStates.size()); before the loop would fix your issue?

My program skips the if statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm writing a program that would make a power of a matrix.
as you see, I'm trying to ask at the for (int n...) loop if n==0,
but when I'm debugging - I see that the program just skips the condition and doesn't even enter it. I mean it doesn't even "ask" the question if n==0...
What is the problem?
void Matrix::pow(int power, Matrix & result)
{
for (int i = 0; i < power-1; i++)
{
for (int j = 0; j < rows; j++)
{
for (int k = 0; k < cols; k++)
{
for (int n = 0; n < cols; n++)
{
if (n==0)
{
(&result)->_array[i][j] == 0; //Reset result's array.
}
(&result)->_array[i][j] += this->_array[i][n] * this->_array[n][j];
}
}
}
}
}
This is a boolean expression, not an assignment.
(&result)->_array[i][j] == 0; //Reset result's array.

Nested For Loop C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
Can some one please point out what is causing the nested for loop to not be executed in the below code sample. The "for (int j = 40; j < =0; j-=5)" loop is not being executed.
void printTable(int windS, int windL)
{
for (int i = windS; i <= windL; i+=5)
{
for (int j = 40; j <=0; j-=5)
{
cout << " " << windChill(j, i);
}
}
}
Thanks in advance.
Because:
for (int j = 40; j <= 0; j -= 5)
will never execute. The j <= 0 will start as 40 <= 0 which results in false.
What you probably meant was:
for (int j = 40; j >= 0; j -= 5)
// ^^
int j=40 is an initializer
then follows condition j <=0 which is never true. You probably meant j>=0
And the action to perform on each iteration j-=5