• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

problem with copying Arrays!!!!! HELP PLEASE!!!

I have an array that was read in under int main()
and it read in 25 integers into an array called src
I have to copy this array into one called dst
I tried to do this:

void copyArray(const int src[], int dst[], int sz0{
     for(int i=0; i < sz; i++){
         dst[i]=src[i];
     }//for loop ends here

Why doesn't this work?  What do I need to do!  This is something I need answered today if possible!  Any help would be appreciated very much!
0
snmeek0
Asked:
snmeek0
1 Solution
 
vmehroCommented:
sz0 or sz?
0
 
snmeek0Author Commented:
It should be sz

my code is as follows:

#include <iostream.h>
#include <fstream.h>
#include <stlib.h>

void copyArray(const int src[], int dst[], int sz);
void printArray(const int A[], int sz, int nPerLine);

int main(){
   ifstream inFile;
   inFile.open("dataFile");
   
if(inFile.fail()){
   cout << "opening failed.  Exiting...\n";
   exit(1);
}

int src[25], dst[25], i , sz=0, A[25], nPerLine;
while(sz<25){
   inFile >> src[i];
   cout << "src[" << sz << "]=" << src[i] << endl;
   sz++;
}//while loop end

copyArray(src, dst, sz);
printArray(A, sz, nPerLine);

}//main

void copyArray(const int src[], int dst[], int sz){
   for(int i=0; i<sz, i++){
       dst[i]=src[i];
       cout << "dst[" << i << "]=" << dst[i] << "  ";
       cout << "src[" << i << "]=" << src[i] << endl;
   }//for loop
}//copyArray
void printArray(const int A[], int sz, int nPerline){
   cout << "enter # array elements to print per line: ";
   cin >> nPerLine;
   for(int j-0; j<nPerLine; j++){
      cout << "A[" << j << "]=" << A[j] << " ";
      if(j==nPerLine){
          cout << endl;
      }//if
   }//for
}//printArray


It reads in everything fine in main.

The output from main is corect.

Then when it gets to void copyArray, the output for dst[0] and src[0] are both 9, which is the last # read in from the file. (ie dst[24] and src[24] both are 9.
All others are 0.  

Where did I go wrong???
0
 
snmeek0Author Commented:
It should be sz

my code is as follows:

#include <iostream.h>
#include <fstream.h>
#include <stlib.h>

void copyArray(const int src[], int dst[], int sz);
void printArray(const int A[], int sz, int nPerLine);

int main(){
   ifstream inFile;
   inFile.open("dataFile");
   
if(inFile.fail()){
   cout << "opening failed.  Exiting...\n";
   exit(1);
}

int src[25], dst[25], i , sz=0, A[25], nPerLine;
while(sz<25){
   inFile >> src[i];
   cout << "src[" << sz << "]=" << src[i] << endl;
   sz++;
}//while loop end

copyArray(src, dst, sz);
printArray(A, sz, nPerLine);

}//main

void copyArray(const int src[], int dst[], int sz){
   for(int i=0; i<sz, i++){
       dst[i]=src[i];
       cout << "dst[" << i << "]=" << dst[i] << "  ";
       cout << "src[" << i << "]=" << src[i] << endl;
   }//for loop
}//copyArray
void printArray(const int A[], int sz, int nPerline){
   cout << "enter # array elements to print per line: ";
   cin >> nPerLine;
   for(int j-0; j<nPerLine; j++){
      cout << "A[" << j << "]=" << A[j] << " ";
      if(j==nPerLine){
          cout << endl;
      }//if
   }//for
}//printArray


It reads in everything fine in main.

The output from main is corect.

Then when it gets to void copyArray, the output for dst[0] and src[0] are both 9, which is the last # read in from the file. (ie dst[24] and src[24] both are 9.
All others are 0.  

Where did I go wrong???
0
 
ozoCommented:
line 28: warning(3665): variable "nPerLine" is used before its
          value is set
       printArray(A, sz, nPerLine);
                         ^

line 33: error(3158): expected a ";"
          for(int i=0; i<sz, i++){
                                ^

line 42: error(3158): expected a ";"
          for(int j-0; j<nPerLine; j++){
                   ^

line 42: warning(3665): variable "j" is used before its value is set
          for(int j-0; j<nPerLine; j++){
                       ^


0
 
The_BrainCommented:
I would do this

while (!input.eof())
{
   src=input.getline(sourcefile);
   strcpy(dst, src, howevermany.)
   
}
just a suggestion.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now