Question

In: Computer Science

#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4]...

#include <iostream>
using namespace std;

void count( int begin[], int end[] )
{
int index, current[4] = { begin[0], begin[1], begin[2], begin[3] };

add:
goto print;

carry:
if ( current[index] < end[index] - 1 ) {
current[index]++;
goto add;
}
else if ( index > 0 ) {
current[index] = begin[index];
index--;
goto carry;
}
else return;

print:
cout << current[0] << current[1] << current[2] << current[3] << endl;
index = 3;
goto carry;

}

int main( )
{
int begin[4] = { 0, 0, 0, 0 };
int end[4] = { 2, 3, 2, 4 };

count(begin, end);

return 0;
}
remove all goto and get the same output
please type code and explain the code. thank you.

Solutions

Expert Solution

If you have any problem with the code feel free to comment.

Program

#include <iostream>
using namespace std;

void count( int begin[], int end[] )
{
    int index, current[4] = { begin[0], begin[1], begin[2], begin[3] };

    //initial print
    cout << current[0] << current[1] << current[2] << current[3] << endl;
    index = 3;
    //infinite loop
    while(1)
    {
        //add condition
        if ( current[index] < end[index] - 1 )
        {
            current[index]++;
            cout << current[0] << current[1] << current[2] << current[3] << endl;
            index = 3;
        }
        //carry condition
        else if ( index > 0 )
        {
            current[index] = begin[index];
            index--;
        }
        //exit the loop
        else
            return;
    }
}

int main( )
{
    int begin[4] = { 0, 0, 0, 0 };
    int end[4] = { 2, 3, 2, 4 };

    count(begin, end);

    return 0;
}

Output


Related Solutions

#include<iostream> using namespace std; void calcSumAndDiff(int ,int, int &, int &); void calcSumAndDiff(int n1,int n2, int...
#include<iostream> using namespace std; void calcSumAndDiff(int ,int, int &, int &); void calcSumAndDiff(int n1,int n2, int &sum, int &diff){ sum = n1 + n2; diff = n1 - n2; } int main() { int n1,n2,sum,diff; n1=30;n2=10; calcSumAndDiff(n1,n2,sum,diff); cout<<"Sum is :"<<sum<<endl; cout<<"Diff is:"<<diff<<endl; system("pause"); }
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count =...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;     cout << "Enter your student ID number: ";     cin >> studentid;     cout << "Student ID Number = " << studentid << endl;     while (studentid != 0)     {          numberreverse[count] = studentid % 10;          if (count == 0)          {              minimum = numberreverse[count];              maximum = minimum;          }          else...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
#include <iostream> #include <cmath>using namespace std; const int A_CONSTANT = 3; void functionA(int a[], int aNumber);...
#include <iostream> #include <cmath>using namespace std; const int A_CONSTANT = 3; void functionA(int a[], int aNumber); void functionB(int a[], int anotherNumber); void functionC(const int anArray[], int aNumber); void functionD(int& sum); int functionE(double number); void functionF(int n); int main( ){ int production[A_CONSTANT]; cout << "This program displays a graph showing\n" << "production for each factory in the company.\n"; functionA(production, A_CONSTANT); functionB(production, A_CONSTANT); functionC(production, A_CONSTANT); return 0;} void functionA(int a[], int aNumber){ for (int someNumber = 1;someNumber <= aNumber; someNumber++) { cout...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
in C++, #include<iostream> using namespace std; const int NUM = 10; void prepareArr(int a[]); int countEven...
in C++, #include<iostream> using namespace std; const int NUM = 10; void prepareArr(int a[]); int countEven (int b[]); int main() { int arr[NUM]; // write a statement to call prepareArr to set values for the array // write a statement to call countEven and print the data returned for(int i = 0; i<NUM; i++) cout << arr[i] <<" "; cout <<endl; return 0; } void prepareArr(int a[]) { //randomly generate NUM integers in the range [0,99] and save them in...
Write the pseudocodes for these programs: a) #include <iostream> using namespace std; void print(int arr[],int n){...
Write the pseudocodes for these programs: a) #include <iostream> using namespace std; void print(int arr[],int n){ if(n==0) return; cout<<arr[n-1]<<" "; print(arr,n-1); } int main() { int n; cout<<"Enter the size of the array:"; cin>>n; int arr[n]; int i; cout<<"Enter the elements of the array:"<<endl; for(i=0;i<n;i++){ cin >> arr[i]; } cout<<"Displaying the elements of the array in reverse order:"<<endl; print(arr,n); return 0; } b) #include<iostream> #include<fstream> using namespace std; //student structure struct student { int id; float gpa; }; //node structure...
#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y);...
#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y); int main(){ char s1[] = "zoey"; char s2[] = "zoey"; string s3 = s1; string s4 = s2; cout << "1. sizeof(s1) = " << sizeof(s1) << endl; if(s1 == s2) cout << "2. s1 == s2 is true" << endl; else cout << "2. s1 == s2 is false" << endl; if(s3 == s4) cout << "3. s3 == s4 is true" <<...
#include<vector> #include<iostream> using namespace std; void println(const vector<int>& v) {    for (int x : v)...
#include<vector> #include<iostream> using namespace std; void println(const vector<int>& v) {    for (int x : v)        cout << x << " ";    cout << endl; } void println(const vector<string>& v) {    for (const string& x : v)        cout << " "<< x << " ";    cout << endl; } int main() {    vector<int> v0;    cout << "An empty vector of integers: ";    println(v0);    vector<int> v1(3);    cout << "A...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT