?
Solved

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

Posted on 1998-11-29
5
Medium Priority
?
179 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 100 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 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