problem facing in filing ....

i m new in c++ my Question is that)
Write a program that reads n numbers (n<1000) from the file “example.txt” (attached) and store it in an array. Ask the user to input n using keyboard at runtime. Then:

a. Compute the sum of the numbers read using any of the loop constructors you have

b. Compute the sum of the numbers read using a recursive function.

learned (for, while or do-while).


I JUST WRITE THIS...

#include <fstream>
#include <iostream>

using namespace std;

int main()
{
int a,b;
int arr[1000];
cout<<"Enter value of n: "<<endl;
cin>>a;
ifstream infile("example.txt")
infile>>a;
.
.
.
I NEED HELP TO COMPLETE MY PROGRAM...
ANYONE COMPLETE MY PROGRAM PLEASE....??
Last edited on
Create a loop from 0 to n
In said loop you should read value from infile to arr[ i ];

After that declare integer variable acc, create another for loop from 0 to n and add value of arr[ i ] to acc

You should know how to make a for loop because your assigment tells:
tahanaqvi wrote:
learned (for, while or do-while).
Last edited on
i have syntax issue i can write pseudo code but i cant write actual code..
n i have to submit this assignment today..

for(i=0;i<=n;i++)
{
.
.
.
1
2
3
4
for(int i=0; i < n; ++i) { //valid indices are 0 through n - 1
    infile >> arr[i];
}
int acc = 0;

//Do next loop youself and calculate sum of array elements
You should test first if the file opened succesfully by member function is_open()

Assuming the numbers are separated by spaces or newlines:

1
2
3
4
5
for ( int i = 0; i < n; i++ ) {
    if ( infile.bad() ) return 0;    
      
    infile >> arr[i];
}


soory for typos earlier
Last edited on
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
int a,b,i;
int arr[i];
cout<<"Enter value of n: "<<endl;
cin>>a;
ifstream infile("example.txt")
infile>>a;
for(int i=0; i < n; ++i)
{
infile >> arr[i];
}
int acc = 0;
for(int i1=0;i1 < n; i1++)
{
acc= acc+arr[i];
}
cout<<acc;


IS IT CORRECT??
IF THEIR IS SOME ERRORS PLEASE REMOVE THEM.... I'LL BE VERY GRATEFUL TO YOU..
the instruction says the you should compute the sum using recursive function

edit
1. what are you trying to do with this line:
infile>>a;

2. you have not declared n, i assume you want n in place of a variable
Last edited on
remove infile >> a;

Everything else is correct. Now open your textbooks and lookup information on second part of your assigment: functions and recursion.
how should i compute the sum by using recursive function?? can u please tell me ..? :(
Calculating factorial using recursion:
1
2
3
4
5
6
unsigned long fact(unsigned long i)
{
    if (i < 2)
        return 1;
    return i * fact(i - 1);
}

You have to pass pointer to first element of the array and array size to your function, and add that element to result of calling same function but with incremented pointer and decremented size.
I know there r many syntax errors here n above in my program.. but i m trying my best..
thanks for ur help..

void sum(arr[i]);
{
int acc=0;
return acc = acc + sum(arr[i+1]);
}

is it correct?
now, please complete my program and remove errors from it
You should pass size of array and use it as sentiel to stop recursion. For now it is infinite.
void sum(arr[i]);
{

int acc=0,i2;
for(i2=0;i2<n;i2++)
return acc = acc + sum(arr[i+1]);
}

how should i stop it...??
sir now please sort my program... :-\
there i create a for loop to stop recursion..
is it right sir? :-/
void sum(arr[i]);
{

int acc=0;
if(i<n)
return acc = acc + sum(arr[i+1]);

}




?????? :(
1
2
3
4
5
6
void sum(int* arr, int n);
{
    if (n = 1)
        return arr[0];
    return arr[0]  + sum(arr+1, n-1);
}
Now, when you are not in danger of not receiving credit, open your books and start learning from the beginning. Also I would suggest dropping C++ if you are not interested in learning it.
Another good tutorials:
http://www.cplusplus.com/doc/tutorial/
http://www.learncpp.com/
Topic archived. No new replies allowed.