how to sum multiple calculations with one string - if-statement

I would need some universal string of commands, so I could insert it in cells A4, C4, D4, E4 and get those desired numbers across all the other similar cells like these.
So far I tried stuff like: =IF(A1 = A2; SUM(A1 * A2); "1") but still not close with that.

What exactly do you want to do?
Not sure why you reference A11 in your formula, or what you expect from SUM(A1 * A2) (do you want A1+A2? or A1*A2?)
Let's assume you want A4 = 1 if A1 = 1 or A2 = 1 and A4 = 0 otherwise. Then type in A4:
=IF(OR(A1=1; A2=1); 1; 0)
Then copy (or right-drag) A4 and paste it in C4, D4 and E4; the formula will adjust to these columns and work the same.
Please clarify your goal if you want better help :)

Related

Subtracting from total if adjacent cell has content

I'm looking to subtract from a total derived from the following:
Count the occurrences of the content in cell E2 =COUNTIF(B:B, E2)
I want to subtract 1 from that total IF an adjacent cell contains anything.
FOr example:
B2 has content == E2, and C2 is NOT empty, subtract 1 from total
B3 has content == E2, and C3 is empty
B4 has content == E2, and C4 is NOT empty, subtract 1 from total
The end result would be =COUNTIF(B:B, E2)-2.
This seemed kind of ridiculous to explain in text so here is an example worksheet with a column expressing where I would like to see the formula, and a column with the expected output. Programatically this would be a very simple task, but doing it in a spreadsheet is new to me. I'm hoping this can be done.
https://docs.google.com/spreadsheets/d/15xEEyeJx1atsZiDDwCk57KfnbCcZbIavV-gyVF0xuCA/edit?usp=sharing
Also, column A and B can vary in length, so I would prefer something that adapts to the length of the column.
delete range D:F
use in D2:
=ARRAYFORMULA(QUERY(QUERY({B1:B,
IF((B1:B<>C1:C)*(A1:A<>"")*(C1:C=""), 1, 0)},
"select Col1,count(Col1),sum(Col2)
where Col1 !=''
group by Col1"),
"offset 1", 0))

Establishing a connection between data given using istringstream and struct/vector

I have the following data in a file, let the columns be [A B C D E]:
a1 b1 c1 d1 e1
a2 b2 c2 d2 e2
a3 b3 c3 d3 e3
.....
All are integers. A is just the serial number. let B be a node having only max of 4 terminals. C represents which terminal of B is taken. Similarly, D is a node having max of 4 terminals and E represents the terminal of D choosen. example: c1 of b1 is connected to e2 of d2.
I have successfully read the data from the files line by line using istringstream and getline as below:
while (getline(infile, line))
{
int i=0;
istringstream iss(line); // string stream
while(getline(iss, temp[i]))
{
cout<< temp[i] << endl;
++i;
};
}
I am not able to conclude on how to save the connections mentions above line to line. I am new to c++ and would appreciate any ideas and implementation. Thanks!
you can just declare a vector<vector<int>> nameVector,(give it a size) and inside of while you put the data. Or be more specific on what you want.

How can I search through a 2D array to make sure that the there are not more than 3 consecutive chars?

I have a 2D array of 3 random chars, a, b, and c. I need to make sure that no column or row has three or more repeating chars.
I'm not really sure how to check the arrays. I think I need to use a loop to go through the array but I'm not sure how to check if there are repeats. To clarify,
if a column has three A's in a row that would not be allowed or could 3 B's be in a column, etc.
So if it is like AAA in 3 consecutive array index (in terms of rows and columns), it should stop.
You need to use for loops if you know the indexes of your array.
so lets say:
char[][] arr= new char[20][20];
for(int i=0; i<18;i++){
for(int j=0; j<18;j++){
if( arr[i][j] ==arr[i][j+1] && arr[i][j] == arr[i][j+2]))
{
break;//mark it as Duplicate
}
else if (arr[i][j] == arr[i+1][j] && arr[i][j] == arr[i+2][j])
{
break;//mark it as Duplicate
}
else
continue;
}
}
So basically you are traversing the array and comparing 3 consecutive elements together. if they are equal it breaks.
You can use an automaton. One to recognize sequences of AAA, BBB and CCC is:
state input next stop
===== ===== ==== ====
0 A a1 no
0 B b1 no
0 C c1 no
a1 A a2 no
a1 B b1 no
a1 C c1 no
b1 A a1 no
b1 B b2 no
b1 C c1 no
c1 A a1 no
c1 B b1 no
c1 C c2 no
a2 A 3 yes
a2 B b1 no
a2 C c1 no
b2 A a1 no
b2 B 3 yes
b2 C c1 no
c2 A a1 no
c2 B b1 no
c2 C 3 yes
3 A 3 yes
3 B 3 yes
3 C 3 yes
you can run this automaton for each row and for each column and stop as soon as you get output yes (it has detected three consecutive letters) You'll get stopped as soon as you recognize the first occurrence of three consecutive letters with only two passes (one for the rows, other for the columns) through the matrix. Of course, initial state is 0.

Dealing from a deck of cards (non repeating cards)

This is a function I have created to try to deal n cards from a deck class of cards. When a card is dealt, it is supposed to be 'removed' from the deck (i.e. can't be dealt again). I thought I solved this by using pointers, and by drawing from the back of the deck minus the amount of cards I have dealt so far. If I call the function again, I need to be drawing from the deck that has cards missing, and since I am not shuffling the deck at any point I thought this method would work.
'dealt' is defined earlier in my code, and starts at 0.
Card is a class that holds a certain suit and value.
I put a sample output of the hands dealt when I call the dealNumber function 6 times and n = 5.
ST CK D3 HJ D9
HK DK SA SQ DT
HK S4 D2 C9 H5
HK H6 H7 H2 H4
HK HK SK S8 C5
HK H9 S3 D8 H8
The 'deck' is ordered this way however:
S6 S2 S7 D7 S9 CQ D4 CA CJ SJ HQ DQ D5 HA DA C4 HT H3 CT D6 C2 S5 H8 D8 S3 H9 C7
C5 S8 SK HK C8 H4 H2 H7 H6 C3 H5 C9 D2 S4 C6 DT SQ SA DK DJ D9 HJ D3 CK ST
The first hand I deal is correct, but the others are not. 'dealt' should be carried over through the deck class regardless, so where am I going wrong?
Hand* Deck::dealingNumber(int p){
Card* dealtCards = new Card[p];
Card* oldDeck = deck;
for (int i = 0; i<n; i++){
dealtCards[i] = deck[(51-dealt)-i];
}
dealt = dealt+n;
deck = new Card[52-dealt];
for (int i = 0; i < 51-dealt;i++){
deck[i] = oldDeck[i];
}
delete[] oldDeck;
PokerHand* hand = new PokerHand(dealtCards);
return hand;
You have an off-by-one error in your second for loop:
for (int i = 0; i < 52 /* change from 51 to 52 */ - dealt; i++){
deck[i] = oldDeck[i];
}
I would take a very different approach using 2 arrays. The first array holds the deck and the second array holds a dealt/not dealt flag.
char cards[52][3]; // 52 cards # 3 characters each
int dealtFlag[52]; // 0=not dealt, 1=dealt
Then choose a random number from 0-51, check that dealtFlag[randNum] is zero, and use that card if it is zero. If it is 1, get a new random number and try again.
Not sure this itself is the problem. Anyway this is a problem:
Modify
for (int i = 0; i < 51-dealt;i++){
deck[i] = oldDeck[i];
}
to
for (int i = 0; i < 52-dealt;i++){
deck[i] = oldDeck[i];
}
Using STL classes for your purpose will be much more simpler.
But personally, I would prefer a stack using linked list or similar.

C++ case declaration? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I am trying to adapt a digital electronics problem to a C++ STL based program.
Originally I have 4 inputs C1, C2, C3, C4. This means I have a total of 16 combinations:
0000
0001
.
.
.
1111
I have a multimap defined by
typedef std::pair<int, int> au_pair; //vertices
typedef std::pair<int, int> acq_pair; //ch qlty
typedef std::multimap<int, acq_pair> au_map;
typedef au_map::iterator It_au;
The no. of simulations depend on the size of the au_map.
For eg: if the au_map.size() = 5 I will have C1, C2, C3, C4, C5. and therefore 2^5 = 32 cases.
For example:
If theau_map.size()=4, I need to simulate my algorithm for 16 cases.
for(It_au it = a_map.begin(); it != a_map.end(); it++)
{
acq_pair it1 = it->second;
//case 0:
//C3 = 0, C2 = 0, C1 = 0, C0 = 0
//Update it1.second with corresponding C values
//simulate algorithm
//case 1:
//C3 = 0, C2 = 0, C1 = 0, C0 = 1
//simulate
.........
//case 15:
//C3 = 1, C2 = 1, C1 = 1, C0 = 1
//simulate
}
Not the best idea. Right now you're doing a lot of useless work by manually setting your C1-C4 and by writing some simulation routines right in your for loop.
Automate it.
Use some abstract State-Simulator mapper (where Simulator actually stands for some concrete functional object).
typedef char State;
struct basic_simulator {
// You could also pass some other parameters to your
// simulator
virtual void operator()(...) = 0
};
struct concrete_simulator : public basic_simulator {
virtual void operator()(...) {
// Do something concrete
// Trolololo
}
};
typedef basic_simulator Simulator;
And in this case your actual wrapper would look like std::map<State, Simulator*> map;
What you need to do next do means getting C1-C4 values from your state which is defined as char. Use bitwise operators.
All your states could be defined as 0-15 numbers converted to binary (2 -> 0010). So to get C1-C4 values you would simply have to make appropriate shifts:
// C1 C2 C3 C4
// -----------
// 1 1 1 1
State state = 15;
for (int i = 3; i >= 0; i--) {
// State of some of the inputs, defined by one bit
InputState C_xx = ((state >> i) & 1);
}
Now simply map all those 0-15 states to appropriate simulating functor:
std::map<State, Simulator*> mapper;
// Generally speaking, you should use some sort of
// smart pointer here
mapper[0] = new concrete_simulator(...);
mapper[1] = ...
What is really cool that you could have only, let's say, 3 or four concrete simulators which would be mapped to some states accordingly.
In this case invoking actual simulation would mean something like:
// Fire appropriate simulation object
(*(mapper[actual_state]))(...);
and making every possible simulation means iterating over every map element.
Update: the same technique could be used for states where you have more than 4 inputs / single input state can have more than two possible values.
Just write an appropriate mapping function and state generator.
Hum... why not letting a for loop enumerate the various combinations for you ?
for (size_t i = 0; i != 16; ++i)
{
bool const c1 = i & 1;
bool const c2 = i & 2;
bool const c3 = i & 4;
bool const c4 = i & 8;
// your algorithm
}
A bit easier than setting them by hand.