I want to blink the LED's in reverse order [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I want to reverse the code as it complete first time. For example pin 1-pin 2-pin 3-pin 4 (it's complete) now it should run as pin 4-pin 3-pin 2-pin 1.
I wrote this code but it's not working in reverse order. Please guide me in this way.
#include<htc.h>
__CONFIG(1,OSCSDIS & HSPLL);
__CONFIG(2,BORDIS & PWRTDIS &WDTDIS);
__CONFIG(3,CCP2RC1);
__CONFIG(4,LVPDIS & STVREN);
__CONFIG(5,UNPROTECT);
__CONFIG(6,WRTEN);
__CONFIG(7,TRU);
define _XTAL_FREQ 40000000
void delay_sec(unsigned char seconds) // This function provides delay in terms of seconds
{
unsigned char i,j;
for(i=0;i<seconds;i++)
for(j=0;j<100;j++)
__delay_ms(10);
}
void led_display(char a)
{
switch(a)
{
case 0: PORTB=0x01;PORTD=0x08; break;
case 1: PORTB=0x02;PORTD=0x04; break;
case 2: PORTB=0x04;PORTD=0x02; break;
case 3: PORTB=0x08;PORTD=0x01; break;
}
}
void main()
{
TRISB=0x00; TRISD=0x00; char a,b;
while(1) {
led_display(a);
a++;
delay_sec(1);
if(a==4) {
a--;
}
}
}

For doing that, you have to remember in which order you are running (reverse or not). So you will have a variable indicating if the order is reverse or not, and change it when you reach the extremities of your counter (0 and 3)
And you can optimize the code with using 1 and -1 for the variable which remembers order and adding it to a.
Your code will seem like this in your main :
int reverse = 1,
char a = 0;
while(1)
{
led_display(a);
delay_sec(1);
if(a==3)
{
reverse=-1;
}
if(a==0)
{
reverse=1;
}
a+=reverse;
}
Regards.

Related

C++ elegant way to combine switch with if and variable assignment [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
My code looks like this
enum possible_cases; //assigned somewhere
bool decision; //assigned somewhere
//basically the default action for my possible_cases
int value = 10;
do_something(value);
switch (possible_cases)
{
case 0:
//assume covered by do_something(value)
break;
case 1:
if ( decision )
{
value = get_other_value();
do_something(value);
}
break;
case 2:
value = get_other_value(); //will return same value as in case 1
do_something(value);
break;
}
As you see
it has to run do_something() with one specific value
it might have to run do_something() with other values additionally, and the list of cases might grow, to a point, where having bools is impractical
but overall I am not happy as it is kinda redundant and I think there is a way to do it better.
I would like to stay with enum of cases
Edit:
It seems not clear where the problem is:
I see the call of the same function in 3 places, while I assume I could reduce it to 2 as I know I need to run it only 2 times at max.
It is really more the aesthetic aspect
The redundant part in your code is do_something(value)
So I would suggest you to separate that part of the code
enum possible_cases; //assigned somewhere
bool decision; //assigned somewhere
//basically the default action for my possible_cases
int value = 10;
switch (possible_cases)
{
case 1:
if ( decision )
value = get_other_value();
break;
case 2:
value = get_other_value(); //will return same value as in case 1
break;
}
do_something(value);
By adding extra layer you might do:
std::optional<int>
get_additionnal_value(Epossible_cases possible_cases, bool decision)
{
//basically the default action for my possible_cases
const int value = 10;
switch (possible_cases)
{
default:
case 0: return std::nullopt;
#if 1
case 1: return decision ? get_other_value() : value;
#else
case 1: if (!decision) return value;
[[fallthrough]];
#endif
case 2: return get_other_value(); //will return same value as in case 1
}
}
and then
do_something(10);
if (auto opt_value = get_additionnal_value(possible_cases, decision)) {
do_something(opt_value);
}

What is the difference between the two codes in C++? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 4 years ago.
Improve this question
I was trying the problem Hash Tables: Ice Cream Parlor on Hackerrank. It is a simple question, but here is the bizzare situation i got to. How does the change of data structure matter?
Case 1:
void whatFlavors(vector<int> cost, int money) {
int ans1,ans2;
vector<int> arr(100,0); //notice this
for(int i=0;i<cost.size();i++){
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
if(ans1>ans2){
cout<<ans2<<" "<<ans1<<endl;
}else{
cout<<ans2<<" "<<ans1<<endl;
}
break;
}
else{
arr[cost[i]]=i+1;
}
}
}
And output is:
Case 2:
code:
void whatFlavors(vector<int> cost, int money) {
int arr[100]={0}; //notice this
int ans1,ans2;
for(int i=0;i<cost.size();i++){
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
if(ans1>ans2){
cout<<ans2<<" "<<ans1<<endl;
}else{
cout<<ans2<<" "<<ans1<<endl;
}
break;
}
else{
arr[cost[i]]=i+1;
}
}
}
output:
Let's just notice this part of your code:
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
This means that your expect money-cost[i] to be negative for some values of i. So you end up reading locations that are outside your array (vector or array) which will lead to undefined behavior in both cases.

Both if and else part are executing [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
/*Q_INVOKABLE*/ void LinkDestUI::selectDeselectSingleDestination(int iXmlId)
{
for(const auto &subNode : m_nodeColl)
{
if(bMultipleSelect())
{
if(subNode->getXmlId() == iXmlId)
{
LOG(Severity_Error)<<"1 ";
subNode->setbSelected(!subNode->bSelected());
}
}
else
{
if(subNode->getXmlId() == iXmlId)
{
LOG(Severity_Error)<<"2 ";
subNode->setbSelected(!subNode->bSelected());
}
else
{
LOG(Severity_Error)<<"3 ";
subNode->setbSelected(false);
}
}
}
}
When i execute my code then compiler goes to else part and exceute both if and else statement inside the else part. why?
That seems ... (temporarily increasing my tactfulness attribute so as to avoid offending) ... rather unlikely :-)
It's probably because the code is being entered more than once and you're just assuming it's executing both parts because of the output.
My advice would be to change the code in the outer else to be:
LOG(Severity_Error)<<"Starting inner if ";
if(subNode->getXmlId() == iXmlId)
{
LOG(Severity_Error)<<"2 ";
subNode->setbSelected(!subNode->bSelected());
}
else
{
LOG(Severity_Error)<<"3 ";
subNode->setbSelected(false);
}
LOG(Severity_Error)<<"Ending inner if ";
You should not see both blocks executing without an intervening end/start message set.

why is this "He is offside!"(spoj) solution giving my WA? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
ideone link: https://ideone.com/hOBBMA
problem link: http://www.spoj.com/problems/OFFSIDE/
code:
enter code here
#include <iostream>
using namespace std;
int main() {
int n1,n2,i,j;
while(1)
{
int count=0;
cin>>n1>>n2;
if(n1==0 && n2==0)
break;
else
{
int a[n1],d[n2];
for(i=0;i<n1;i++)
{
cin>>a[i];
}
for(i=0;i<n2;i++)
{
cin>>d[i];
}
for(i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
if(a[i]>d[j])
{
count++;
}
}
}
}
if(count>=2)
cout<<"N"<<endl;
else
cout<<"Y"<<endl;
}
return 0;
}
it gives the correct answer with the given test cases but apparently it's a WA
Consider this input; two attackers at 400m from the goal and two defenders also at 400m from the goal. Your code would count '0' i.e. offside whilst according to the rules neither of the attackers isn't.
An attacking player is offside if he is nearer to his opponents’ goal
line than the second last opponent. [This is not true for any of the attackers in this case so definitely not an offside]
A player is not offside if he is level with the second last opponent [In this case they are level so certainly not offside] or he is level with the last two opponents [And this is true too].
2 2
400 400
400 400
0 0

How to keep track of order in which switch case values are executed? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I keep track of the order in which switch case statements are executed?
For example:
while (some_boundary) {
switch (value) {
case a:
do something;
move to next value;
break;
case b:
do something;
move to next value;
break;
case c:
do something;
move to next value;
break;
}
}
I want to know if the switch was executed abc or bac or cab, etc.
Any idea ? Or will implementing via if/else make more sense ?
You can save a vector at every iteration with the value of the corresponding iteration:
std::vector<int> sequence;
while (some_boundary) {
int temp = computeValue(); // Or however you get your value.
sequence.push_back(temp);
switch (temp) {
case a:
//do something;
break;
case b:
//do something;
break;
case c:
//do something;
break;
}
}
Edit: This is assuming that value is set somewhere between the while and the switch, so you can save it in advance. Other option is to include the push_back instruction in every case, but is more "dirty". Preallocating the vector could save some computation time as well.
Edit2: code modified according the suggestions so that it is ensured that a new value will be computed.
If you just want to know (and not save the results) you could just output the value at each iteration. Try
cout << value << endl;
as the first line within the while loop.