C++ array relative C question

H

hpy_awad

I know that group is for C not for C++ but please try to help me ,
How can I declare size of an array as variable value came from screen?

#include "iostream.h"
#include "iomanip.h"
#include "conio.h"
float ArraySum(float Array[],int ArraySize);
int main()
{
clrscr();
int ArraySize=0;
// Read the array elements
cout << "Enter Array Size : " ;
cin >> ArraySize;
^^^^^^^^^^^^^^^^^^^^^
float Array[ArraySize];
^^^^^^^^^^^^^^^^^^^^^^^
cout <<endl<< "Enter Array Elements " ;
for (int i=0;i<=ArraySize;i++)
{
cout <<endl<< "Enter elements no " << i << " : ";
cin >> Array;
}
cout <<endl << "The sum of the elements values :" << setw(5) <<
ArraySum(Array,ArraySize);
return 0;
}

float ArraySum(float arr[],int n)
{
int total=0;
for (int i=0;i<=n;i++)
total+=arr;
return total;
}
 
M

Mark A. Odell

(e-mail address removed) ([email protected]) wrote in

I know that group is for C not for C++ but please try to help me ,
How can I declare size of an array as variable value came from screen?

You cannot. You must malloc() the memory and use a pointer instead of an
array. E.g.

float *pArray = malloc(ArraySize * sizeof *pArray);
 
M

Michael Mair

Hi there,

I know that group is for C not for C++ but please try to help me ,

The question is: What do you want to do?
Do you want to do it in C++, C89, C99?
We can help you only with the last two -- and, topically, only
with portable code.

How can I declare size of an array as variable value came from screen?

In all three languages: malloc() (and free())
C++: new (and delete)
C89: malloc() (and free())
C99: malloc() (and free()), variable length array

#include "iostream.h"
#include "iomanip.h"
#include "conio.h"
float ArraySum(float Array[],int ArraySize);
int main()
{
clrscr();
int ArraySize=0;
note: use size_t for everything you want to use as array index
// Read the array elements
cout << "Enter Array Size : " ;
cin >> ArraySize;
^^^^^^^^^^^^^^^^^^^^^
float Array[ArraySize];
^^^^^^^^^^^^^^^^^^^^^^^
in C99, this line is perfectly valid
C89:
float *Array;
at beginning of main() with the other declarations, and
Array = malloc(ArraySize * sizeof *Array);
if (Array == NULL) {
/* Handle error, usually exit(EXIT_FAILURE). */
}
and at the end -- or as soon as you no longer need the memory
Array is now pointing to --
free(Array);
cout <<endl<< "Enter Array Elements " ;
for (int i=0;i<=ArraySize;i++)
{
cout <<endl<< "Enter elements no " << i << " : ";
cin >> Array;
}
cout <<endl << "The sum of the elements values :" << setw(5) <<
ArraySum(Array,ArraySize);
return 0;
}

float ArraySum(float arr[],int n)
{
int total=0;
for (int i=0;i<=n;i++)
total+=arr;
return total;
}


Note: For C99 VLAs, there are special options when passing them
as arguments.


If you can specify your needs somewhat clearer, we can help you
better.
It would also be better to give us C code instead of C++ code.


Cheers
Michael
 
D

Dave

I know that group is for C not for C++ but please try to help me ,

Suppose you were in a Maths class, and the lecturer asked "Any
questions?" and someone piped up "I have this question about my Physics
homework."

It's not a case of whether or not the mathematicians present know
physics; in many cases they will, but they are there to discuss Maths,
not Physics, and the latter is a waste of everyone's time.

As is discussing C++ in comp.lang.c. Does your newsgroup software
somehow not have access to comp.lang.c++?

C and C++ are substantially different languages, even though they both
begin with the same letter (like C and Cobol, or PL/1 and PL/SQL), and
discussions about the details of one can develop into substantial
bandwidth hogs, even if this wasn't intended by the OP, so please
restrict your posting to On Topic items only.

Dave.
 
M

Martin Ambuhl

I know that group is for C not for C++

So why not post to comp.lang.c++, where C++ is topical.
but please try to help me ,
How can I declare size of an array as variable value came from screen?

The OP's code is quoted at EOM. Here is a rewrite. Note that many
current implementations will not like it. Luckily, the comp.lang.c FAQ
and all texts for beginners give full explanations of the way to do this
that works for all C compilers.

#include <stdio.h>
#include <stdlib.h>

double ArraySum(double Array[], int ArraySize);

int main()
{
unsigned ArraySize = 0, i;
printf("Enter Array Size: ");
fflush(stdout);
scanf("%u", &ArraySize); /* see the FAQ for better ways to do
input safely */
double Array[ArraySize]; /* With many C compilers this will not
work. If using ISO C89, declare the
pointer-to-double Array at the top
of the block, malloc the space,
checking the return value. You may
find that preferable even if the
code given works. The FAQ gives good
coverage to the right way to do
that, as will any elementary text
for beginning programmers.

Notice that the broken "<=" in the
loop below and in ArraySum are
fixed. */
printf("Enter Array Elements ..\n");
for (i = 0; i < ArraySize; i++) {
printf(" Elements #%u: ", i);
scanf("%lg", &Array); /* see the FAQ for better ways to
do input safely */
}
printf("The sum of the elements values : %g\n",
ArraySum(Array, ArraySize));
return 0;
}

double ArraySum(double arr[], int n)
{
int total = 0, i;
for (i = 0; i < n; i++)
total += arr;
return total;
}

#include "iostream.h"
#include "iomanip.h"
#include "conio.h"
float ArraySum(float Array[],int ArraySize);
int main()
{
clrscr();
int ArraySize=0;
// Read the array elements
cout << "Enter Array Size : " ;
cin >> ArraySize;
^^^^^^^^^^^^^^^^^^^^^
float Array[ArraySize];
^^^^^^^^^^^^^^^^^^^^^^^
cout <<endl<< "Enter Array Elements " ;
for (int i=0;i<=ArraySize;i++)
{
cout <<endl<< "Enter elements no " << i << " : ";
cin >> Array;
}
cout <<endl << "The sum of the elements values :" << setw(5) <<
ArraySum(Array,ArraySize);
return 0;
}

float ArraySum(float arr[],int n)
{
int total=0;
for (int i=0;i<=n;i++)
total+=arr;
return total;
}
 
K

Karthik Kumar

I know that group is for C not for C++ but please try to help me ,
How can I declare size of an array as variable value came from screen?


From your comments, it seems like you are writing C++ code.
Hence setting follow-up to c.l.c++ .

#include "iostream.h"
#include "iomanip.h"

They are deprecated headers as far as C++ is concerned.

#include <iostream>
#include <iomanip>




#include "conio.h"

This is non-standard header.
float ArraySum(float Array[],int ArraySize);
int main()
{
clrscr();
int ArraySize=0;
// Read the array elements
cout << "Enter Array Size : " ;

The compiler (C++ ) would have flagged an error here since
cout belongs to std namespace.

cin >> ArraySize;

Ditto for cin again.
^^^^^^^^^^^^^^^^^^^^^
float Array[ArraySize];
^^^^^^^^^^^^^^^^^^^^^^^


Nope. Use dynamic memory allocation.
cout <<endl<< "Enter Array Elements " ;
for (int i=0;i<=ArraySize;i++)
{
cout <<endl<< "Enter elements no " << i << " : ";
cin >> Array;
}
cout <<endl << "The sum of the elements values :" << setw(5) <<
ArraySum(Array,ArraySize);
return 0;
}

float ArraySum(float arr[],int n)
{
int total=0;
for (int i=0;i<=n;i++)


Please use size_t for array indices.
total+=arr;
return total;
}
 
M

Mabden

Dave said:
Suppose you were in a Maths class, and the lecturer asked "Any
questions?" and someone piped up "I have this question about my Physics
homework."

It's not a case of whether or not the mathematicians present know
physics; in many cases they will, but they are there to discuss Maths,
not Physics, and the latter is a waste of everyone's time.

As is discussing C++ in comp.lang.c. Does your newsgroup software
somehow not have access to comp.lang.c++?

C and C++ are substantially different languages, even though they both
begin with the same letter (like C and Cobol, or PL/1 and PL/SQL), and
discussions about the details of one can develop into substantial
bandwidth hogs, even if this wasn't intended by the OP, so please
restrict your posting to On Topic items only.


Maths? There's more than one?
We just call it Math on this continent.
 
M

Michael Mair

Mabden said:
Dave said:
Suppose you were in a Maths class,
[snip]

Maths? There's more than one?
We just call it Math on this continent.

Look it up. It is called mathematics, short maths
(add scary capital letters when needed), as a subject as well as
a discipline. I find "math [Amer.] [ifml.]: maths" when looking
up "math"...

I do not know which continent you are from but I guess that
you did not pay attention in your *English* class.

--Michael
 
C

Christopher Benson-Manica

Michael Mair said:
Look it up. It is called mathematics, short maths
(add scary capital letters when needed), as a subject as well as
a discipline. I find "math [Amer.] [ifml.]: maths" when looking
up "math"...

I did (dictionary.com) and maths comes up as "chiefly British".
I do not know which continent you are from but I guess that
you did not pay attention in your *English* class.

Probably from the same one I'm from, where the subject is nearly
universally referred to as "math". While the Queen might disagree,
that's the way it is.
 
M

Mark McIntyre

Probably from the same one I'm from, where the subject is nearly
universally referred to as "math". While the Queen might disagree,
that's the way it is.

While I'm sorry for you, the Rest of the World can't be held responsible
for the USA's inability to even write english, let alone speak it... :)

(By the way, I have quite a problem understanding how you can truncate a
plural word to a singular one, without asking yourselves "which particular
bit of maths?". But then, the US was mostly colonised by ethnic cleansing
of one sort or another, so its unfair to also expect them to bother with
stuff like grammar and spelling.)
 
M

Mabden

Mark McIntyre said:
While I'm sorry for you, the Rest of the World can't be held responsible
for the USA's inability to even write english, let alone speak it... :)

(By the way, I have quite a problem understanding how you can truncate a
plural word to a singular one, without asking yourselves "which particular
bit of maths?". But then, the US was mostly colonised by ethnic cleansing
of one sort or another, so its unfair to also expect them to bother with
stuff like grammar and spelling.)

Ah, abuse.

Math is singular, as is Moose, or to make it relevant to this newsgroup:
Code. When someone refers to "the software codes" one generally realizes
it is a clueles newbie talking. Code is the sum of all the lines
written, there is no plural of code.

As for speaking properly, there are rules in the language that govern
how words should be spoken. For instance, "later" and "latter". a vowel
one consonant away from a letter makes it sound like Fonzie's "Ay"
(ape), but two away makes it sound like "ah" (car). So how do you say
"tomato"? It is clearly t-O (oh!)-m-A(Ay!)-to, not t-O-mah-to.

Also, there is no F in think.

Silly English Kuh-nig-its.
 
C

Chris McDonald

Math is singular, as is Moose, or to make it relevant to this newsgroup:
Code. When someone refers to "the software codes" one generally realizes
it is a clueles newbie talking. Code is the sum of all the lines
written, there is no plural of code.

The choice of 'math' over 'maths' is very much a regional choice.
Preaching on which is 'correct' is incorrect.
The OED provides 3 definitions for 'math':

1. A mowing; the action or work of mowing; that which may be or has been
mowed; the portion of a crop that has been mowed.

2. In South Asia: a monastery, esp. one for celibate Hindu mendicants.

3. N. Amer. colloq. Mathematics (esp. as a subject of study at school or
college). Cf. MATHS n. (the usual British colloquial abbreviation).

Australians invariably choose 'maths' as a contraction of mathematics.
Also, there is no F in think.

There is no P in swimming.

______________________________________________________________________________
Dr Chris McDonald E: (e-mail address removed)
Computer Science & Software Engineering W: http://www.csse.uwa.edu.au/~chris
The University of Western Australia, M002 T: +618 6488 2533
Crawley, Western Australia, 6009 F: +618 6488 1089
 
M

Michael Mair

Benson-Manica said:
While I'm sorry for you, the Rest of the World can't be held responsible
for the USA's inability to even write english, let alone speak it... :)

Oh, they are from the USA...
The only logical solution would have been that they are from
Australia, talking about a continent -- but, as someone else
pointed out, people in Australia know about maths :)
I would like to recall the fact that there also is Canada on
the same continent as the USA even if this comes as a
surprise. Maybe we can also get a Canadian opinion on this
topic...


[snip]
But then, the US was mostly colonised by ethnic cleansing
of one sort or another, so its unfair to also expect them to bother with
stuff like grammar and spelling.)

Well, in the case of Mabden:
Better leave his grandmothers alone and do not admit
to be a witch or he might misunderstand you ;-)


Cheers,
Michael
 
M

Mark McIntyre

Ah, abuse.

nope. Humour. Mind you, since you can't even spell the word..... :)
Math is singular,

Sure. Thats my point. Mathematics isn't.
As for speaking properly, there are rules in the language that govern
how words should be spoken.

Mhm.
For instance, "later" and "latter". a vowel
one consonant away from a letter makes it sound like Fonzie's "Ay"
(ape), but two away makes it sound like "ah" (car).

Consonant. From. Letter. It. One. All of these break the above rule....
whoops.
Of do you pronounce them "Cone-sone ay-nt, Fr-oh-m, lee-teer, aye-t,
oe-ne"?
So how do you say
"tomato"? It is clearly t-O (oh!)-m-A(Ay!)-to, not t-O-mah-to.

Only to the ignorant. :)
Also, there is no F in think.

Ah dinnae ken whit yer babblin aboot, so why disnae ye hadawaeanshite, ye
great ploater?
 
M

Mabden

Michael Mair said:
While I'm sorry for you, the Rest of the World can't be held responsible
for the USA's inability to even write english, let alone speak it...
:)

Oh, they are from the USA...
The only logical solution would have been that they are from
Australia, talking about a continent -- but, as someone else
pointed out, people in Australia know about maths :)
I would like to recall the fact that there also is Canada on
the same continent as the USA even if this comes as a
surprise. Maybe we can also get a Canadian opinion on this
topic...


[snip]
But then, the US was mostly colonised by ethnic cleansing
of one sort or another, so its unfair to also expect them to bother with
stuff like grammar and spelling.)

No snipping, since it is quite fragmented already. My Canadian opionion
is that we call the Math. No plural. Sorry if I'm such an asshole that I
sound American ;-) BTW, we share a continent, hence my use of the term,
instead of country.

So, are you saying that Australia has only its native peoples?

Or that London is full of English people (what was your dinner tonight,
a curry)?
Well, in the case of Mabden:
Better leave his grandmothers alone and do not admit
to be a witch or he might misunderstand you ;-)

What the hell does the grandmother / witch stuff mean??? Am I offended
or just stupid? I honestly DO misunderstand.

You seem to think that the people who have come to the USA are lesser
people than you yourself. I detect a bias for the "English" in you. You
seem to classify people as "English" and "Ethnic", and you seem to
believe that the "Ethnic" people are a sub-class of people. Apparently
in your world, "those people" can't write or spell. You seem to think
they are not really humans but animals who could better themselves, if
only they still allowed you to whip some sense into them.
 
M

Mabden

Mark McIntyre said:
nope. Humour. Mind you, since you can't even spell the word..... :)

???

You mean because Americans spell "color" that way? You don't know
anything about me or how I spell anything.
Sure. Thats my point. Mathematics isn't.

Yes it is. Mathematics is one concept. You wouldn't say, "Those
mathematics..." as if it were many things. "I was learning those
mathematics the other day...", "I teach those mathematics to the
kids..."
Even in YOUR bass-ackward country they don't do that, do they?
Consonant. From. Letter. It. One. All of these break the above rule....
whoops.
Of do you pronounce them "Cone-sone ay-nt, Fr-oh-m, lee-teer, aye-t,
oe-ne"?


"Consonant" and "One" you got me on (Doh!). "From" has no other vowel,
neither do the others. You obviously don't understan how English works,
or my explanation of how it works if you think "Letter" should be
"Leeter". The double consonant makes it a soft e, not a hard e.

Only to the ignorant. :)
:p



Ah dinnae ken whit yer babblin aboot, so why disnae ye hadawaeanshite, ye
great ploater?

Ah, more abuse.

And it's Spaghetti with Meat Sauce, not Bollocks-naise, you damn Limey
Bastards!!! ;-)
 
J

Joona I Palaste

Ah, more abuse.
And it's Spaghetti with Meat Sauce, not Bollocks-naise, you damn Limey
Bastards!!! ;-)

Does "Limey" mean "Englishman" or "Briton"? It's one or the other, but
I have never found out which. If it's "Englishman" then Mabden's
accusation is wrong, as I think Mark is Scottish.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top