Can I have some help anyone?

I have this program and I can't figure it out. Help please!


Last edited on
I can't figure it out.

And what is "it"?
T
Last edited on
Lol, now that is a question
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#define MAXBITS 10
#include <iostream> // Note
using namespace std; // Note


void Flip(char *x, int Last, int step)
{
char swap[2] = {'1','0'}; // Note

int k;
for(k=0;k<=Last;k+=step)
{

x[k] = swap[x[k]-'0'];
}
}

int main()
{
char bits[MAXBITS];
// char swap[2] = {'1','0'}; Note
int k;
for(k=0;k<MAXBITS-1;k++)
{
bits[k]='0'+rand()%2;
}
bits[k]='\0';

Flip(&bits[0],9,1); //I suspect my function call is bad

cout<<endl;
cout <<"Enter a character to quit ";
char quit;
cin >> quit;
return 0;
}
Around line 10, in your flip function you refer to a variable swap:
swap[x[k]-'0'];
I does not appear to be defined in that scope.

Flip(&bits[0],9,1); //I suspect my function call is bad
Here you are passing the address of an array of chars, that is, you are passing a char**. Drop the &.
Sorry...just a beginner in programming!
You need to make swap global. So move the declaration from main to the top of the program above Flip(). Also, as it never changes, you make it constant:
 
const char swap[2] = { '1', '2' };
Thank you all for helping!
I dropped & but it still not working.
Everything in the code is working except the function call Flip which I had to write but I can"t pass the argument in the function without interfeering with swap.
I did but still no working...I get the error
`swap' undeclared (first use this function)

The program is supposed to work without any changes...I have to only write the function Flip
I tried passing many and sorts of arguments into function but is not working
If it needs to work without changes then why are you asking for help with changing it?

using namespace std;

Last edited on
I did but still no working...I get the error
`swap' undeclared (first use this function)
Have you considered my advice above?

The program is supposed to work without any changes... I have to only write the function Flip
In which case, ignore my advice above. A swap is something we do all the time:
http://en.wikipedia.org/wiki/Swap_%28computer_science%29
I just needed to write the function Flip but my codding is not working
All you have to do is replace
//x[k] = swap[x[k]-'0'];//I commented this out to get an output
with
x[k] = '1' - (x[k] - '0');
and replace
Flip(&bits[0],9,1); //this is the problem
with
Flip(bits, 9, 1);
Last edited on
Still no working...does it work for you when you compiled? I can't pass the compiler step
What exactly is the error the compiler is giving you (copy and paste), and on what line of code (also copy and paste)?
Last edited on
Topic archived. No new replies allowed.