GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Nov-2007, 19:31
Efferus Efferus is offline
New Member
 
Join Date: Nov 2007
Posts: 5
Efferus is on a distinguished road

Need Help with input files.


As the title states I need help with a program. The problem is that an array can't be declared with n as the size; n doesn't have a value at the time of the declaration, so the array can't be created. I have no idea how or where to start to fix it. The file is as follows:

CPP / C++ / C Code:
#include <iostream>
#include <fstream>
using namespace std;
void readdata(int &,int [],int []);
void printarrays(int ,int []);
void findlimits(int,int [],int &,int &);
void construct(int , int [],int [],int []);
void whatshigher(int [],int [],int,int,int,int);
ofstream out15;
ifstream in15;
int main()

{
    int n,largest,smallest,higher,equal,lower;
    int test1[n],test2[n],test3[n];
        
    readdata(n,test1,test2);
    out15<<"The test1 array ";
    printarrays(n,test1);
    out15<<"the test2 array ";
    printarrays(n,test2);
    findlimits(n,test1,largest,smallest);
    out15<<" largest in test 1 is " <<largest<<" smallest is " <<smallest;
    findlimits(n,test2,largest,smallest);
    out15<<" largest in test 2 is " <<largest<<" smallest is " <<smallest;
    out15<<" the test3 array ";
    construct(n,test1,test2,test3);
    whatshigher(test1,test2,n,higher,equal,lower);
    system("pause");
}    
    

//read data
void readdata(int &n,int val1[],int val2[])
{
     for(int i=0;i<n;i++){
          in15>>val1[i];
          in15>>val2[i];                     
     }
     return;
}

//print arrays
void printarrays(int lim,int vals[])
{
     for ( int i = 0; i < lim; i++ )
         out15 << vals[ i ] << " ";
     out15 << endl;
}
    
//Find Limits
void findlimits(int num,int arr[],int &max,int &min)
{
     min = arr[0], max = min;
     for (int count=1;count<num;count++){
     if (arr[count] < min)
     min = arr[count];
                else if (arr[count] > max)
                        max = arr[count];
        }
}

//construct
void construct(int k,int arr1[],int arr2[],int newarr[])
{
     in15>>k;
     for(int i=0;i<k;i++){
          in15>>arr1[i];
          in15>>arr2[i];                     
     }
     for(int i=0;i<k;i++){
     newarr[i]=arr1[i]+arr2[i];
     }printarrays(k,newarr);
}

//whats higher
void whatshigher(int test1[],int test2[],int n,int higher,int equal,int lower)
{
     for(int i=0;i<n;i++){
             if(test1[i]>test2[i])
             out15<<"test 1,in position "<< i << "the higher value is in test 1 "<<test1[i]<<"is greater than "<<test2[i];
             else
                  if(test2[i]>test1[i])
             out15<<"test 2,in position "<< i << "the higher value is in test 2 "<<test2[i]<<"is greater than "<<test1[i];
             }
}
Last edited by admin II : 23-Nov-2007 at 05:53. Reason: Please surround your C++ code with [cpp] your code [/cpp]
  #2  
Old 21-Nov-2007, 21:08
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,122
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Need Help with input files.


Well, you will have more [minor] issues, but as a start, to fix this first question, you could possibly change this code:
CPP / C++ / C Code:
int n,largest,smallest,higher,equal,lower;
to something like this:
CPP / C++ / C Code:
const int n = 10;
int largest = 0, smallest = 0, higher = 0, equal = 0, lower = 0;
// always try to initialize the variables. Avoids subtle bugs,
// and provides a KNOWN start value.
Also, this change will require changing the first argument type of function readdata(), replace:
CPP / C++ / C Code:
int &
with
CPP / C++ / C Code:
const int
Since the function doesn't do anything to 'n' [only uses it in the loop evaluation] there is no need to have it as a reference.
Actually, since 'n' is now constant, anywhere this is passed as an argument should also have the const keyword added, as none of your functions modify the value of 'n'.

Oh, and please post any future code between [c++] ...paste your code here... [/c++] tags.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 24-Nov-2007, 17:19
Efferus Efferus is offline
New Member
 
Join Date: Nov 2007
Posts: 5
Efferus is on a distinguished road

Re: Need Help with input files.


I was wondering if there is a way to keep the first argument a reference parameter. This is actually an assignment I have and the main program is described. And then the assignment goes on to describe what the functions should do. I tried my best so far by first using cin instead of an infile to test the program but the program stated crashing when I used an input file.
I'll make the assignment in italics to make it easier to differentiate them from my comments.
The Assignment is as follows:

1. The main program will call a function readdata() to read in a parameter value (which both readdata() and the main program will call n). The function will then read n items into two arrays (which the main program will call test1 and test2). All data values, including the parameter value, must be read from the input file.
2. The main program will call a function printarray() (twice, once for array test1 and once for array test2) which will print the values stored in the two arrays. Each time before the main program calls the printing function, it should print a heading saying what is being printed (for example, it could say “the test1 array”). All output, including headings, should go to a file.
3. For each array, the main program will call a function findlimits() which will find both the largest and the smallest of the n values stored in an array. The main program will call the function once to find the largest and smallest of the n values stored in the test1 array, and once to find the largest and smallest of the n values stored in the test2 array. The main program will pass to findlimits() the reference parameters largest and smallest to get values inside the function. The main program will print largest and smallest, together with relevant messages.
4. The main program will call a function construct(). The function will construct a new array (which the main program will call test3) from the two arrays test1 and test2. Then construct() will call printarray() to print test3. Either main or the function construct() can print the heading for array test3.
5. The main program will call the function findlimits() again to find the smallest of the n values stored in the test3 array. The main program will print this value.
6. The main program will call a function whatshigher(). This function will be used to compare the contents of the test1 and test2 arrays. The function will receive six parameters: two arrays (called test1 and test2), an integer n (giving the size of the two arrays), and three counters, which are reference parameters. The function will compare the two arrays, element by element, to calculate in which array the value is higher. In addition, the function changes its three counter parameters; after the function returns to main, main will print the counters, together with clear messages.


The functions should do the following:
A. The function readdata() will read the data into two arrays. The function will receive three parameters: an integer n, and two arrays which it calls val1 and val2. At the beginning, n will have no value, and the two arrays will be empty. The value of n will be used to control the action of the function, as described below. All parameters to readdata() are reference parameters.

The function will read a value into in n, and then read n lines of data. Each line of data should contain two integers; the first will be read into val1, and the second will be read into val2. (The values stored in n and in the two arrays will be sent back to the main program, to be used throughout the program.)
B. The function printarray() will receive two parameters, an integer lim, and an array called vals. It will print the lim values stored in the array vals, all on one line, with spaces between the values.
C. The function findlimits() will receive four parameters, an integer num, an array called arr, and two parameters which it will call max and min. It will place the smallest of the first num elements of the arr array into min and the largest into max. The variables max and min are reference parameters.
D. The function construct() will receive four parameters: an integer k, and three arrays called arr1, arr2, and newarr. The function will construct the first k elements of the newarr array from the first k elements of the arr1 and arr2 arrays, as follows. Each element of newarr will be equal to the corresponding element of old1 plus the corresponding element of arr2. For example, newarr[0] will be arr1[0] + arr2[0], newarr[1] will be arr1[1] + arr2[1] and so on.

After constructing the array, and printing a heading, the function construct() should call the function printarray() to print the new array. Note that construct(), not main, calls printarray().
E. The function whatshigher() will receive six parameters: two arrays (called test1 and test2), an integer n (giving the size of the two arrays), and three counters. The function will compare the two arrays, element by element, to calculate in which array the value is higher. For each array element, the function will print which array has the higher value (or if the two values are equal). The function should print each of the values, the array it is from, and the relationship.
In addition, the function changes its three counter parameters: the three counters tell how many times the value from test1 is larger, how many times the value from the test1 is smaller, and how many times the values in the two arrays are equal. The counters should get all their values, including their initial values, in the functions.


Now, I know that I'm suppose to also make the function construct() have three counter tokens and I think I know how to do that but what I'm really clueless about is how to maintain a n reference parameter for the readdata() function.
If possible can anyone help me?
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
malloc/free usage, and general good programming behavior lavaka C Programming Language 7 14-Jun-2007 20:50
HELP!!!! Input files/ if then Clezzie C++ Forum 0 11-Feb-2006 17:39
Using files for input crystalattice C++ Forum 15 31-Aug-2004 19:53
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 03:26
IP tables rogermark100 C Programming Language 6 18-Apr-2004 08:22

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:53.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.