1. Write a program that requires you to enter eight integers in the array named
temp.
2. When entering each number, add the number to the total.
3. After entering all numbers, the program shows:
(a) the array created
(b) the sum of all elements
(c) the mean value of the array elements.
// 1. Write a program that requires you to enter eight integers in the array named usrinpt
#include <iostream> /* cout, cin, ... */
usingnamespace std;
int main()
{
int usrinpt[8], total; // an array, a variable, all null
cout << "Enter eight integers pls. ";
// 2. When entering each number, add the number to the total.
cin >> usrinpt[0];
total = usrinpt[0];
cin >> usrinpt[1];
total += usrinpt[1];
cin >> usrinpt[2];
total += usrinpt[2];
cin >> usrinpt[3];
total += usrinpt[3];
cin >> usrinpt[4];
total += usrinpt[4];
cin >> usrinpt[5];
total += usrinpt[5];
cin >> usrinpt[6];
total += usrinpt[6];
cin >> usrinpt[7];
total += usrinpt[7];
// 3. After entering all numbers, the program shows:
cout << "\nAND NOW! ...drum roll... the results:";
// (a) the array created
cout << "\nThe array:" << ' ' << usrinpt[0] << ' ' << usrinpt[1] << ' ' << usrinpt[2] << ' ' << usrinpt[3] << ' ' << usrinpt[4] << ' ' << usrinpt[5] << ' ' << usrinpt[6] << ' ' << usrinpt[7];
// (b) the sum of all elements
cout << "\nThe total: " << total;
// (c) the mean value of the array elements.
cout << "\nThe mean: " << total / 8.;
}
Enter eight integers pls. 1 2 3 4 5 6 7 8
AND NOW! ...drum roll... the results:
The array: 1 2 3 4 5 6 7 8
The total: 36
The mean: 4.5
Question: is there something like an implicit loop over all elements of the array, for example usrinpt[*] or usrinpt[0..7] or usrinpt[alpha():omega()] to get all at once?
@dhayden
This is not what I ment with an implicit loop. This is a special case of a for loop which gets its arguments from an object's properties, the array in this case.
I ment something like cout << "Content of the array: " << array[*];
to append the complete array to the literal "Content of...".
Something similar like toString of ooREXX:
a = .array~of(1,2,3,4) -- Loads the array
say a~toString -- Produces: 1
-- 2
-- 3
-- 4
say a~toString("c") -- Produces: 1234
say a~toString(, ", ") -- Produces: 1, 2, 3, 4
dunno how 'implicit' it is -- that depends on how you read it. Just like your language explicitly says * (meaning all elements) this one has begin and end (meaning all elements) which explicitly enumerates a loop if you know how to read it. But there isnt a loop written there, so now we are down to semantics.
its doing the same thing as an explicit loop, and its cryptic as all get out, so I don't know why you would want to do this.
there may be a cleaner way to write that as well. I hacked up an example. Never had to use all of before.
this is not the intended use of all_of. this is abusing all_of to hide a loop that should have been a range based for loop.
Thank you, it is 'comparable implicit' as ooREXX toString. Of course, behind the curtains it's looping.
It is more or less about cosmetics/aesthetics, like swap( a, b ) instead of c = a; a = b; b = c;
You can type code and it gets interpreted/executed as you proceed.
You can debug by printing current values of variables.
C++ is a compiled language. You don't have values when you type the logic. If you want the program to print something, you decide how it does that. There could be a default, but to agree on one requires more than it is worth*.
Yes, it is. (There is a compiler for it on IBM mainframes, but speed-up is small to mean, best at calculations.)
You can type code and it gets interpreted/executed as you proceed.
No (unless you are in an interacitve trace "session"), typically you type code in an editor, before executing it you have to file or save the code.
You can debug by printing current values of variables.
REXX knows only one type of variables: text. (In case you do calculations the math routines first check if the text could be interpreted as a number. (A slow interpreter like REXX has plenty of time to do so, for heavy number crunching you would use other systems.)
C++ is a compiled language. You don't have values when you type the logic.
From my point of view it would be enough to have the values at runtime.
There could be a default, but to agree on one requires more than it is worth
Ok, got it. Another language, another culture. I restrain from quoting Goethe in this context ;)
part of OOP is detail hiding, really.
and you can use a microclass to wrap a vector or array with a cout friend to print it the way you want it printed, then you can just say
cout << yourarraylikeclass_variable;
and it will do it, if it makes you feel better. With a template and ctor you can set the text it wraps around the array elements and control its behaviors. If you need more than that you can add methods that let you configure it more on demand.
there are several other ways to do it in c++. for-all and the ranged for loop are 2 ways that the language gives you already. If those do not work for you, you can build what you seek or try some other smoke and mirrors.
I would caution you about writing a boatload of code that does nothing but add overhead and convolution to make it look like another language or to eliminate/reduce lines of code. Making the code more readable with your constructs should be a goal, sure, but that should be a natural by product of making your objects, not the point of the objects themselves. Once in a while, sure, but if you are doing it all the time to the point of creating your own language, its probably counterproductive.
... you can use a microclass to wrap a vector or array ... and it will do it, if it makes you feel better.
Well, "make me feel better" is not decisive, my question was foremost -- before I start reinventing the wheel -- "is there something like an implicit loop", not could or should or may I do it. With C++ being so widely used I hoped there was someone before me who done it.
In fact I like (original) REXX for its limited set of instructions which forces to slice a task until every slice may be coded. In contrast, other languages offer a multitude of sometimes quite similar commands so it is (for me) quite hard not to overlook a better option.
I would caution you about writing a boatload of code that does nothing but add overhead and convolution ...
Don't worry, not my goal. I prefere to have it unmistakable, this will help a lot when you look again at your code few weeks later. But, honestly, C++ helps to discombobulate simple minds, what was it recently... quite funny...
you can do your pipelines (not exactly, but similar enough to do the work). its really just handoff via shared memory & multiprocessing server type stuff. Could also be ye olde unix chaining, where the output of one program feeds another into a third and so on, which c++ programs can do with just cin/cout to use that interface. Even windows has this, though its less used there.
there isn't much c++ can't do, honestly. It may take a great deal of work to mimic what some focused languages can do, and someone may or may not yet have done that work, but most things are doable.
Its not going to look like what you had, but that is just syntax.
you can do your pipelines (not exactly, but similar enough to do the work).
Yes, sure, I can do. i) no, I can't, ii) others tried already, but did not achieve something adequate to John's Pipelines on VM/CMS. There is an IBM internal attempt on OS/2 and Windows from 2001, looks quite promising, alas it stays unfinished.
So if you say "you can do" it's a little overstatement. Unfortunately ;)
Yes, sure. And if you start today it could be finished Easter. Easter Monday latest. Easter Monday 2020.
Alas, VM/CMS is not OS/2 is not Windows. That could be one hurdle, another one, much higher IMO is of pecuniary nature -- there is probably nobody who will pay money for your effort. Some connoisseurs will pay attention, few will applaude, I would say thank you. Easter Monday 2020 -- promised :)
one person, easter maybe 2050 or worse. I am not unrealistic, I am just looking at whether it can be done or not from the technical aspect. The other question is whether it should be done. There is probably a reason this more or less died on the vine... and its not technical difficulty in implementing it (we have plenty of insanely overly complex systems from the 80s and 90s that all you can do is shake your head at now). It looks like it adds a ton of overhead and slowness to gain ... plug and play coding (which exists, though its mostly terribad).