Hi , I'm new at C++ and I want to demonstrate you that :D
I'm doing very hard to understand Arrays, I read them in 3 books and I'm not getting them up, I watched the examples at cplusplus.com too. I thought lets try make a program that uses array's and maybe I can understand them then. I thougt of a program that finds the average of numbers I type(it would be easy with forever loop but I want to understand the Arrays). I made what I could but the program doesn't work it took me hours trying to figure it out and watching other programs how they are made with arrays and I modificated the program many times and yet I'm not sure what it is wrong(I have no ideas more tried all), so maybe the program is totally wrong and not only a small part sfter many modifications. I would be very glad if anyone explains me what I've done wrong(I hope it isn't at all wrong:)) and what I have to change so than it works.
Here the module:
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int division(int , int);
int main(int nNumberofArgs, char* pszArgs[])
{
cout << "Find the avarage of numbers u type, Negative number to stop." <<endl;
int a[128];
{
int i;
for(i = 0; i < 128; i++)
{
int sum;
cout << "Numbers u want the average of: ";
cin >> sum ;
if(sum<0)
{
break;
}
a[i] =sum;
}
cout << "=" << division(a, i)
<< endl;
system ("PAUSE");
return 0;
}
int division(int b[], int i)
{
int accumulator ;
for (int b = 0; b < i; b++)
{
accumulator = b/[i];
}
return accumulator;
}
It seems like you have weak concepts and you are advancing forward. You should study 3rd and fourth chapters of "C++ How to Program" by Deitel. Here is better code(not the best).
#include<iostream>
usingnamespace std;
int division(int,int);
main()
{
int size=5; //Array size
int array[size]; //Declaring array
int sum=0;
for(int i=0;i<size;i++) //Loop which inputs arrays data and
//Calculates its sum
{
cout<<"Enter element number "<<i+1<<endl;
cin>>array[i];
sum=sum+array[i];
}
//Now calling division function to find the average...
cout<<"Average of array elements is "<<division(sum,size);
}
int division(int sum,int size)
{
return sum/size;
}
, thanks mate it works , It's almost how i wanted it to be, i changed 7th line and it is almost perfect:
int size;
cout << "How many numbers u want the average of:" ;
cin >> size;
I have too 'C++ how to program' by Deitel , but I was using 'C++ for Dummies-5th edition'. Is deitel's book better ?
English is not my natal language it does my learning even harder:)
Keep it up. one day you will be a good programmer. Don't lose heart. Deitel's book is easy to understand. Try it. "Study slowly but steadily". As this website is not offering summer course, so you can't get continuous help for a single program. But yes this website is really helpful if you know the basics of C++. Be in contact...
#include<iostream>
usingnamespace std;
int division(int,int);
int main()
{
int size=5; //Array size
int array[size]; //Declaring array
int sum=0;
for(int i=0;i<size;i++) //Loop which inputs arrays data and
//Calculates its sum
{
cout<<"Enter element number "<<i+1<<endl;
cin>>array[i];
sum=sum+array[i];
}
//Now calling division function to find the sum...
cout<<"Average of array elements is "<<division(sum,size);
return 0;
}
int division(int sum,int size)
{
return sum/size;
}