Noob, trying to learn C++

Hi Folks,
I am hoping for a little bit of direction. The program below is just a test program to create arrays based on input of two numbers. The numbers are only 0 to 9 in each array. They can only be either even or odd number but not mixed. My issue currently is figuring out how to use a struct and a function that allows me to select each array without duplicating the function 5 times (ie: b3,b4,b5,b6,b7).
Any help will be greatly appreciated.
Thanks Rick


Output would be:
b6 = {4,6,8,0}
b7 = {7,9,1,3}


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
37
38
39
40
41
42
43
44
45
46
47
//test program to build array
#include <cstdio>
using namespace std;

struct B {
    int bit[4] = {0};
}b3, b4, b5, b6, b7;

void bLoad6 (int tnum, int bnum){
    for (int x = 0; x<4;x++)
    {
        b6.bit[x] = tnum == bnum ? tnum +=2 : tnum;
        b6.bit[x] = tnum > 9 ? tnum -=10 : tnum;
        b6.bit[x] = tnum == bnum ? tnum +=2 : tnum;
        tnum +=2;
    }
}

void bLoad7 (int tnum, int bnum){
    for (int x = 0; x<4;x++)
    {
        b7.bit[x] = tnum == bnum ? tnum +=2 : tnum;
        b7.bit[x] = tnum > 9 ? tnum -=10 : tnum;
        b7.bit[x] = tnum == bnum ? tnum +=2 : tnum;
        tnum +=2;
    }
}
//Main Program
int main()
{
    //Load array with 4 numbers
    bLoad6(2, 2);
    bLoad7(5, 5); //I don't want to have 5 simple functions. Just one

    // print array b6
    for (int x = 0; x<4;x++)
    {
       printf("bit6[%d] = %d\n", x+1, b6.bit[x]);
    }
    printf("\n\n");
    for (int x = 0; x<4;x++)
    {
       printf("bit7[%d] = %d\n", x+1, b7.bit[x]);
    }
    return 0;
} //End of Main
@rrowan327,

Could you possibly have another go at explaining what you are trying to achieve.

The following code produces the same output as your program ... but I don't understand what you are asking.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

void load( int a[4], int base )
{
   for ( int i = 1; i <= 4; i++ ) a[i-1] = ( base + i * 2 ) % 10;
}


int main()
{
   int odd[4], even[4];
   load( even, 2 );
   load( odd , 5 );

   for ( int i = 0; i < 4; i++ ) cout << "bit6[" << i+1 << "] = " << even[i] << '\n';
   cout << "\n\n";
   for ( int i = 0; i < 4; i++ ) cout << "bit7[" << i+1 << "] = " << odd [i] << '\n';
}


bit6[1] = 4
bit6[2] = 6
bit6[3] = 8
bit6[4] = 0


bit7[1] = 7
bit7[2] = 9
bit7[3] = 1
bit7[4] = 3
Last edited on
hi lastchance,

That works great! Much easier than what I was trying to do. I was basically looking for a way for one function to build the 5 different arrays with different sequence of numbers. I never thought of the modulus operator. I have a Looong way to go. LOL

I mod you code slightly to this.
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
#include <iostream>
using namespace std;

void load( int a[4], int base )
{
   for ( int i = 1; i <= 4; i++ ) a[i-1] = ( base + i * 2 ) % 10;
}


int main()
{
   int b3[4], b4[4], b5[4], b6[4], b7[4];
   load( b3, 1 );
   load( b4, 3 );
   load( b5, 9 );
   load( b6, 2 );
   load( b7, 5 );

   for ( int i = 0; i < 4; i++ ) cout << "bit3[" << i+1 << "] = " << b3[i] << '\n';
   cout << "\n\n";
   for ( int i = 0; i < 4; i++ ) cout << "bit4[" << i+1 << "] = " << b4[i] << '\n';
   cout << "\n\n";
   for ( int i = 0; i < 4; i++ ) cout << "bit5[" << i+1 << "] = " << b5[i] << '\n';
   cout << "\n\n";
   for ( int i = 0; i < 4; i++ ) cout << "bit6[" << i+1 << "] = " << b6[i] << '\n';
   cout << "\n\n";
   for ( int i = 0; i < 4; i++ ) cout << "bit7[" << i+1 << "] = " << b7[i] << '\n';
}


Many Thanks
Rick


Last edited on
High complexity as this might result with a lot of errors among the code and unless you're using some external tools as checkmarx it might going to be a hard job to detect those.
Make sure you try to decrease the amount of complexity in the code.
Good luck.
Registered users can post here. Sign in or register to post.