473,943 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ array relative C question

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<=ArraySiz e;i++)
{
cout <<endl<< "Enter elements no " << i << " : ";
cin >> Array[i];
}
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[i];
return total;
}
Nov 14 '05 #1
39 2043
eh***********@y ahoo.com (hp******@yahoo .com) wrote in
news:7e******** *************** ***@posting.goo gle.com:
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(ArraySiz e * sizeof *pArray);

--
- Mark ->
--
Nov 14 '05 #2
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(ArraySiz e * sizeof *Array);
if (Array == NULL) {
/* Handle error, usually exit(EXIT_FAILU RE). */
}
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<=ArraySiz e;i++)
{
cout <<endl<< "Enter elements no " << i << " : ";
cin >> Array[i];
}
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[i];
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

Nov 14 '05 #3


hp******@yahoo. com wrote:
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.
Nov 14 '05 #4
hp******@yahoo. com wrote:
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[i]); /* 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[i];
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<=ArraySiz e;i++)
{
cout <<endl<< "Enter elements no " << i << " : ";
cin >> Array[i];
}
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[i];
return total;
}

Nov 14 '05 #5
hp******@yahoo. com wrote:
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<=ArraySiz e;i++)
{
cout <<endl<< "Enter elements no " << i << " : ";
cin >> Array[i];
}
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[i];
return total;
}

--
Karthik.
http://akktech.blogspot.com .
Nov 14 '05 #6
hp******@yahoo. com wrote:
I know that group is for C not for C++ but please try to help me ,

Why? What is wrong with comp.lang.c++?


Brian Rodenborn
Nov 14 '05 #7
"Dave" <re************ *@elcaro.moc> wrote in message
news:ii******** ****@news.oracl e.com...


hp******@yahoo. com wrote:
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.

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

--
Mabden
Nov 14 '05 #8
"Default User" <fi********@boe ing.com.invalid > wrote in message
news:I5******** @news.boeing.co m...
hp******@yahoo. com wrote:
I know that group is for C not for C++ but please try to help me ,

Why? What is wrong with comp.lang.c++?


Well, don't _even_ get me started about THOSE pricks!

;-)

--
Mabden
p.s. These are the jokes, folks...
p.p.s. I know you're there, I can hear you breathing.
Nov 14 '05 #9
Mabden wrote:
"Dave" <re************ *@elcaro.moc> wrote
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

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1348
by: Radhika Sambamurti | last post by:
Hi, I'm a relative newbie.... I've written a program to do a bubble sort. take in numbers and sort them. My question is: at the very end of the sort() function, when I have to output the result to stdout, the only code that does this correctly is as follows: //output of sorted array for(i=1; i<n+1; i++) { cout << "value of i is: " << num_array << endl;
15
6265
by: fdunne2 | last post by:
The following C-code implements a simple FIR filter: //realtime filter demo #include <stdio.h> #include <stdlib.h> //function defination float rtFilter1(float *num, float *den, float *xPrev, float *yPrev);
19
3101
by: Geetesh | last post by:
Recently i saw a code in which there was a structer defination similar as bellow: struct foo { int dummy1; int dummy2; int last }; In application the above array is always allocated at runtime using malloc.In this last member of the structer "int last" is not
21
3269
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a comma delimited list of values var jsData = new Array(); jsData = {lib: "#field...
33
3448
by: Benjamin M. Stocks | last post by:
Hello all, I've heard differing opinions on this and would like a definitive answer on this once and for all. If I have an array of 4 1-byte values where index 0 is the least signficant byte of a 4-byte value. Can I use the arithmatic shift operators to hide the endian-ness of the underlying processor when assembling a native 4-byte value like follows: unsigned int integerValue; unsigned char byteArray;
49
2219
by: David | last post by:
I need to do an array with around 15000 integers in it. I have declared it as unsigned letter; But the compiler is saying that this is too much. How can I create an array for 15000 integers? I am using "Miracle C compiler". This is the error i am recieving: "c:\documents and settings\david\desktop\asdasdasdasdasdasdasdad22.c:
15
6488
by: Lars Eighner | last post by:
Aside from the deaths of a few extra electrons to spell out the whole root relative path, is there any down side? It seems to me that theoretically it shouldn't make any difference, and it would make it much easier to slap modualar blocks of markup into page frameworks, which may change and so forth. And the few extra bytes, which even for a fairly large site would not amount to as many bytes as are in a fairly small low-res image, should...
2
5229
by: berrylthird | last post by:
This question was inspired by scripting languages such as JavaScript. In JavaScript, I can access members of a class using array syntax as in the following example: var myInstance:myClass = new myClass(); myInstance.member_0 = memberValue_0; // absolute notation myInstance = memberValue_0; // relative notation myInstance = memberValue_0; // nominal notation You can initialize a struct in C/C++ like you would an array, as with the...
4
2480
by: somenath | last post by:
I have a question regarding the memory allocation of array. For example int array; Now according to my understanding 10 subsequent memory location will be allocated of size sizeof(int) *10 and this is done at compile time. Does it mean C compiler reserve some amount of memory for the array
0
10136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9970
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11126
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10662
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9865
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8220
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3512
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.