Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program to generate all and only three-digit number from 000 to 999 the sum of whose digits is odd. So, for example, your

Write a program to generate all and only three-digit number from 000 to 999 the sum of whose digits is odd. So, for example, your program won’t print 000 or 246, but it would print 001 and 342.

How?

Use the backtracking method that we used for the 8 queens problem (and 8 numbers in a cross, and stable marriage, and missionaries …).

Integer array q[3] will represent the solutions (for example the number 11 will be represented in the array as:[0,1,1]. Use an ok() function to test for validity and use gotos to control the program flow as we did in class.

Here is an outline of the structure of the 8 queens program:

bool ok(   some parameters go here ){

Code goes here

}

int main(){

// declarations and initializations of array q, c , …etc. go here

nc: c++;

if(         )     goto print;

q[c]= ________________________

nr: q[c]++;

            if (                    ) goto backtrack;

// test if ok

            if( not ok() ) goto nr;

// passed all tests

goto nc;

backtrack:c--;

            if(c==-1)return 0;

            goto nr;

           

print:

    // print goes here

            goto backtrack;

}


1). Describe your algorithm for generating the required numbers using the data structure (array q) and the backtracking method that we first developed with the 8 queens problem.

Your description should generally follow the code outline above.

Step by Step Solution

3.50 Rating (150 Votes )

There are 3 Steps involved in it

Step: 1

include using namespace std bool ok... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Financial Management

Authors: William R. Lasher

7th edition

128560721X, 9781133593669, 1133593682, 9781285607214, 978-1133593683

More Books

Students also viewed these Programming questions

Question

What argument was made against adopting FASB?

Answered: 1 week ago