Solved

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

Posted on 1998-11-29
5
176 Views
Last Modified: 2011-09-20
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
Comment
Question by:snmeek0
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Accepted Solution

by:
vmehro earned 50 total points
ID: 1178811
sz0 or sz?
0
 

Author Comment

by:snmeek0
ID: 1178812
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
 

Author Comment

by:snmeek0
ID: 1178813
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
 
LVL 84

Expert Comment

by:ozo
ID: 1178814
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
 
LVL 1

Expert Comment

by:The_Brain
ID: 1178815
I would do this

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question