Computer Science 161

Professor Valente

An Example of C++ progamming

 

The Algorithm:

            Set the value of Sum to 0

            Get a value for X

            While X is not 0

                        Set the value of Sum to Sum + X

                        Get a value for X

            End While

            Print the value of Sum

            Stop

 

Our VSC-32 assembly language program to solve this problem:

            LOAD Zero

            STORE Sum

            READ X

While: LOAD X

            JUMP Print

            LOAD Sum

            ADD X

            STORE Sum

            READ X

            LOAD Zero

            JUMP While

Print:   WRITE Sum

            STOP

            Zero: DATA 0

            Sum: DATA 0

            X: DATA 0

 

A C++ program to solve this problem:

            main() {

                        int x, sum = 0;

                        while (x != 0)

{

                                    sum += x;

                                    cin >> x;

                        }         

                        cout << “sum = ” << sum;

            }