-
your opinions???
Hi..:)
In my simple program, i want to accept array of numbers of any size and have to return its sum.
Whether i should accept that array in the defined function of sum which is:
Code:
void sum(int ar[], const int size)
or this accepting array should be in main??
~~>Please i'm new to c++
-
If by "accept" you mean "read input so as to populate the array", then I would say that the code for that should not be in the sum function. Perhaps it could go into the main function, or perhaps you could create another function for that.
By the way, since it looks like you are going to use a dynamic array, look into the use of a std::vector<int> instead.
-
Hello laserlight..
Yes i want to get input from the user, and i wrote the code as you said that it should be in the main function.
This is what i've done yet.
Code:
#include <iostream.h>
#include <conio.h>
void sum(int a[],const int size);
void main()
{
int ar[];
const int size;
for(int i=0 ;i<size; i++)
{
cin>>ar[i];
}
sum();
getch();
}
void sum(int a[], const int size)
{
int s=0;
for(int i=0;i<size;i++)
{
s=s+a[i];
}
cout<<s;
}
And i'm getting errors in it.
Will you tell what should i do?
Where i'm calling function "sum()" i've a confusion here.
-
no size
You should really use vectors here as suggested, but in any case you declare your const int size, but it is not assigned any value, also in sum() you are creating a local variable anyway, you are not passing your original array in, so even if it did have any size and contents in main(), this would be irrelevant in sum()
-
thanks for the reply rogster001 :)
What if i assigned arrays size of 5 and after summing in the sum function, can i call it in main as sum(ar[5])??
Can i do it in this way if i don't want to use vectors??
Code:
#include <iostream.h>
#include <conio.h>
int sum(int a[5]);
int main()
{
int ar[5];
for(int i=0 ;i<=5; i++)
{
cin>>ar[i];
}
sum(ar[5]);
return 0;
}
int sum(int a[5])
{
int s=0;
for(int i=0;i<=5;i++)
{
s=s+a[i];
}
return s;
}
-
Well there are two problems with your understanding of arrays. First, you wrote for (i = 0; i <= 5; i++) as the loop, which will process six elements when there are only five. Recall that arrays are numbered from 0 to n-1 in C++, so perhaps you can fix your array accordingly.
Second, sum(arr[5]); is very wrong. arr[5] is an expression which evaluates to a single integer. Not only that but if we look at the prototype for sum, it is this:
Did you know that is equivalent to this?
Basically 'a' is a pointer that you will use to access all of arr in main. There is no real information about the length of the array being passed to this function, and the compiler will not make sure that an array of five elements is being passed. When dealing with functions that process arrays, the usual thing is to pass a pointer and a length variable.
If you're not going to use more than five elements or so though, there's nothing wrong with what you have. Arrays are not dynamic structures. What you do is use new[] and delete[] to create dynamic arrays with pointers. But C++ also has an even easier alternative called vector.
Code:
while (cin >> temp && temp != -1) myvector.push_back (temp);
int theSum = sum (reinterpret_cast<int*> (&myvector[0]) , myvector.size ());
And you've got an array sum(int *a, size_t size) can work with, except that vector manages the array's memory and cleans up automatically after itself.
-
thanks..
I think i've to grip on my concepts. I've tried book "How to program C++" by deitel (6th edition)
would you please tell me which one is the best book for C++??
so that i can clear all my concepts about functions,vectors e.t.c as i've not used vectors before. I'm new for this.
any help will be appreciated..
-
I've heard of the Deitel book, so just read what you have. I know it covers vectors, so you might want to read ahead to where the book starts covering it. If you're interested in other books or what we think about your book, please search in the C++ Book Recommendations thread.
-
> #include <iostream.h>
> #include <conio.h>
Lemme guess, TurboC user right?
You know, this is well over a decade past being obsolete.
-
hi..
I'm using Borland C++ 5.02 as recommended to me in my college. I'm in third semester and i'm new to c++. I shouldn't use it??
Will you please guide me Salem??
-
if your course is structured around the borland then its hard to tell you to change but you may become reliant on library functions that you wont find elsewhere as you advance and portability issues also as i understand it, maybe it was suggested as its free but its over ten years old and there are better free alternatives like my pref codeblocks but you will find others in help pages
-
If you begin a program with
Code:
#include <iostream>
using namespace std;
Does it complain at all?
If it does complain, then you might want to ask your tutors why you're being taught something which is obsolete.
One of the points of going to college is to come out at the front of the technology curve, not way behind.
-
@rogster001: Ok. I want to ask you about best book for c++ for the newbie like me.
Is Summita Arrora's C++ book is best one for absolute beginners??
I urgently needed the best book for C++ because i think i'm totally ignorant to C++.
@Salem: My teacher didn't tell me to use using namespace std;. I just add #include <iostream.h> and #include <conio.h>.
-
You can read the book recommendation thread, but for a beginner I would recommend either Accelerated C++ by Koenig and Moo or You Can Do It!: A Beginners Introduction to Computer Programming by Glassborow.
-
@laserlight: Thankyou. I'll try both.. :)