Solved

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

Posted on 1998-11-29
5
171 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
5 Comments
 

Accepted Solution

by:
vmehro earned 50 total points
Comment Utility
sz0 or sz?
0
 

Author Comment

by:snmeek0
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I would do this

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now