Logo 
Search:

C Programming Forum

Ask Question   UnAnswered
Home » Forum » C Programming       RSS Feeds

Need help with my source code.

  Asked By: Agustin    Date: Oct 30    Category: C Programming    Views: 2262
  

I need to create a program to read an array of doubles from the keyboard, calculate the sum, and print the array and its sum. In main(), declare a double array that can hold up
to 50 doubles and an integer to store the current size (initialize this to 0). Then call the getInput() and
printArray() functions.

This is the pseudo code.

How many doubles do you wish to add? -5
That is an invalid size. You must enter a value between 1 and 50.
How many doubles do you wish to add? 5
Enter double 1: 1
Enter double 2: 1.5
Enter double 3: 2
Enter double 4: 3.2
Enter double 5: 2.5
The array contains:
1.00 1.50 2.00 3.20 2.50
The sum is 10.2

Here is what i have so far. I am missing a lot. Any help will be appreciated.

#include <iostream>
using namespace std;

void getInput( double[], int& );
void printArray(double[], int& );

int main()
{
const int array_size = 50;
double arr[array_size] = 0;
int length;

return 0;
}

void getInput( double num[], int& n )
{
cout << "How many doubles do you wish to add?"
cin >> n;

for(int length; i <= n; i++)
{
cin >> n;
}

}

void printArray( double num[], int& n )
{

}

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Madhusudhan Gowda     Answered On: Mar 26

#include<iostream.h>
#include<conio.h>
Void getinput(double [],int&);
void pointarray(double [],int&);
int m;double a[100];
clrscr();
cout<<"enter array size";
cin>>m;
if(m<=0)
cout<<"invalid;
else{
getinput(a,m);
pointarray(a,m);
}
getch();
}
void getinput(double a[],int &m)
{
double sum=0;int i;
for(i=1;i<=m;i++)
{
cout<<"enter double";<<i<<":\t";
cin>>a[i-1];
}
for(i=0;i<m;i++)
{
sum=sum+a[i];
}
cout<<"the sum is:"<<sum;
}
void pointarray(double a[],int &m)
{
cout<<"the array contains:\n";
for(int i=0;i<m;i++)
{
cout<<a[i]<<"\t";
}}

 
Answer #2    Answered By: Madhusudhan Gowda     Answered On: Mar 26

#include<iostream.h>
#include<conio.h>
Void getinput(double [],int&);
void pointarray(double [],int&);
int m;double a[100];
clrscr();
cout<<"enter array size";
cin>>m;
if(m<=0)
cout<<"invalid;
else{
getinput(a,m);
pointarray(a,m);
}
getch();
}
void getinput(double a[],int &m)
{
double sum=0;int i;
for(i=1;i<=m;i++)
{
cout<<"enter double";<<i<<":\t";
cin>>a[i-1];
}
for(i=0;i<m;i++)
{
sum=sum+a[i];
}
cout<<"the sum is:"<<sum;
}
void pointarray(double a[],int &m)
{
cout<<"the array contains:\n";
for(int i=0;i<m;i++)
{
cout<<a[i]<<"\t";
}}

 
Didn't find what you were looking for? Find more on Need help with my source code. Or get search suggestion and latest updates.




Tagged: