COMPUTER ORGANISATION AND ASSEMBLY LANGUAGE

Can anyone help with this program.


Write a program that takes input of 5 numbers from the user and print the sum of the elements in the array?
Skkahn wrote:
Can anyone help with this program.
Write a program that takes input of 5 numbers from the user and print the sum of the elements in the array?


Sure.

1
2
3
4
5
program sum5
   integer A(5)
   read *, A
   print *, sum( A )
end program sum5


a.exe
1 3 5 7 9
          25

https://onlinegdb.com/HJgsjsACS
Last edited on
Can anyone help with this program.

What program?

If you've written some source you need to post it here. We can offer hints and suggestions for fixes and improvements then.

IF you want someone to write the entire program for you, post in the Jobs forum.

It will be expensive.
COMPUTER ORGANISATION AND ASSEMBLY LANGUAGE
Why? Why is this the title of your post? Is that the name of your class? A book? Are you writing assembly?

Also, Furry Guy is too discouraging, I'll do it for a low rate of $3 USD per (logical, properly formatted) level of indentation. :)

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <string>
#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cstdlib>
#include <stdexcept> 
#include <memory>

template <typename T>
using array = std::vector<T>; 

class InfinityException : public std::exception {
  public:
    const char* what() const noexcept override;
    ~InfinityException() override { }
};

const char* InfinityException::what() const noexcept
{
    return "Infinite numbers are not allowed";
}

class NaNException : public std::exception {
  public:
    const char* what() const noexcept override;
    ~NaNException() override { }
};

const char* NaNException::what() const noexcept
{
    return "Numbers that aren't numbers are not allowed";
}

class INumber {
  public:
    virtual double getValue() const = 0;
    virtual void setValue(const INumber&) = 0;
    virtual ~INumber() { }
};

class IntNumber : public INumber {
  public:
    IntNumber(int value)
    : value(value) {}
    double getValue() const override;
    void setValue(const INumber& number) override;
    
  private:
    int value;
};

double IntNumber::getValue() const
{
    return this->value;
}

void IntNumber::setValue(const INumber& number)
{
    this->value = static_cast<int>(number.getValue());
}

class DoubleNumber : public INumber {
  public:
    DoubleNumber(double value)
    : value(value) {}
    double getValue() const override;
    void setValue(const INumber& number) override;

  private:
    double value;
};

double DoubleNumber::getValue() const
{
    return this->value;
}

void DoubleNumber::setValue(const INumber& number)
{
    this->value = number.getValue();
}

std::unique_ptr<INumber> operator+(const std::unique_ptr<INumber>& a,
                                   const std::unique_ptr<INumber>& b)
{
    // TOOD: virtual operator overloading?
    // IntNumber + IntNumber --> IntNumber?
    return std::make_unique<DoubleNumber>(a->getValue() + b->getValue());
}

class INumberFactory {
  public:
    virtual std::unique_ptr<INumber> makeNumber(const std::string& value) const = 0;
};

std::string tolower(std::string data)
{
    std::transform(data.begin(), data.end(), data.begin(),
        [](unsigned char c){ return std::tolower(c); });
    return data;
}

class NumberFactory : public INumberFactory {
  public:
    std::unique_ptr<INumber> makeNumber(const std::string& value) const override;    
    virtual ~NumberFactory() { }
};

std::unique_ptr<INumber> NumberFactory::makeNumber(const std::string& value) const
{
    if (tolower(value) == "inf" || tolower(value) == "+inf" || tolower(value) == "-inf")
    {
        throw InfinityException();
    }
    else if (tolower(value) == "nan" || tolower(value) == "+nan" || tolower(value) == "-nan")
    {
        throw NaNException();
    }
    else if (value.find('.') == std::string::npos)
    {
        std::unique_ptr<INumber> number = std::make_unique<IntNumber>(std::stoi(value));
        return number;
    }
    else
    {
        std::unique_ptr<INumber> number = std::make_unique<DoubleNumber>(std::stod(value));
        return number;
    }
}

int main()
{
    std::unique_ptr<INumberFactory> factory = std::make_unique<NumberFactory>();
    
    constexpr std::size_t NumNumbers = 5;
    
    std::cout << "Enter " << NumNumbers << " numbers: ";

    array<std::unique_ptr<INumber>> numbers;
    for (std::size_t i = 0; i < NumNumbers; i++) // TODO: Remove raw loop!
    {
        std::string number_str;
        std::cin >> number_str;
        
        try
        {      
            std::unique_ptr<INumber> pNumber = factory->makeNumber(number_str);       
            numbers.push_back(std::move(pNumber));
        }
        catch (const InfinityException& inf_ex)
        {
            std::cout << "Invalid number entered: " << inf_ex.what()
                      << ". Terminating program.\n";
            return 1;
        }
        catch (const NaNException& nan_ex)
        {
            std::cout << "Invalid number entered: " << nan_ex.what()
                      << ". Terminating program.\n";
            return 2;
        }
        catch (...)
        {
            std::cout << "Invalid number entered. Terminating program.\n";
            return 3;
        }
    }

    std::unique_ptr<INumber> starting = std::make_unique<DoubleNumber>(0.0);
    std::unique_ptr<INumber> sum = std::accumulate(numbers.begin(), numbers.end(),
        std::move(starting), std::plus<std::unique_ptr<INumber>>());
    
    std::cout << sum->getValue() << '\n';
    
    return 0;
}

Actually, I changed my mind. It's free. 'Tis the season for giving, and I feel generous. Merry Christmas.
Last edited on
Topic archived. No new replies allowed.