Need help on for loop and array

This is a discussion on Need help on for loop and array within the C++ Programming forums, part of the General Programming Boards category; How can I change this code so it will output for two or more values in an array like vec[vSize]={5000,3000,4000}; ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    Need help on for loop and array

    How can I change this code so it will output for two or more values in an array like vec[vSize]={5000,3000,4000}; instead of one value so in this case I would get three outputs for each timing of the for loop?
    Code:
    #include< iostream >
    #include< iomanip >
    #include< cstdlib >
    #include< ctime >
    #include< cstdio >
    #include< fstream >
    
    const int vSize = 1;
    using namespace std;
    
    
    
    int main()
    {
       
        int vec[vSize]={5000};//,50000//,100000,200000,400000};
    
        //Declare the clock variables
        clock_t start,finish;
        clock_t startAll;
    
        //Declare and initialize all the variables
       double duration;
        
        //Declare the streams and the input and output files
        ifstream inFile;
       ofstream outFile;
    
        inFile.open("input1.txt");
       outFile.open("A:/output1.txt");
    
        
    int Sum=0;
        
         
        
         for (int iterationcountselector = 0; iterationcountselector < vSize; ++iterationcountselector)
        {
             start=clock();
             for(int i =0; i < vSize ; i++)
             {
                  Sum++;
                  outFile<< Sum<<endl;
             }
        }
    
        /*     finish=clock();
             duration=(double)(finish-start)/CLOCKS_PER_SEC;
             cout<<"          "<< setw(4)<< duration;*/
             
        
              finish=clock();
             duration=(double)(finish-start)/CLOCKS_PER_SEC;
             cout<<"Total Time Duration"<< duration<<" ms"<< endl;
    
        
         
        for (iterationcountselector = 0; iterationcountselector < vSize; ++iterationcountselector)
        {
             
        
              for(int i =0; i < vSize; i++)
             {
                  Sum++;
                  outFile<< Sum<<endl;
             }
        }
    
             finish=clock();
             duration=(double)(finish-start)/CLOCKS_PER_SEC;
             cout<<"Total Time Duration"<< duration<<" ms"<< endl;
        
         return 0;
    }//end of main

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Im not sure exactly what you mean, but here is an example of how to print a whole line of array elements then go to the next line and print the second set of array elements.
    Code:
    for (x=0;x<10;x++)
    {
    	for(y=0;y<10;y++)
    	{
    		cout << y << " ";
    	}
    	cout << "." << endl;
    }
    
    output:
    1 2 3 4 5 6 7 8 9 10.
    1 2 3 4 5 6 7 8 9 10.
    
    instead of 
    1
    2 
    3 
    4
    etc....
    The keyboard is the standard device used to cause computer errors!

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    Clarify

    I am trying to time a for loop for different values in a array. My output is the amount of time the for loop runs for an array value.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You changed your inner loop from your last post. You want to compare i to the current value of the vec array, as indexed by the outer loop. So change your inner loop to check for i < vec[iterationcountselector]. Notice how the outer loop controls the inner loop when you use that value as the index.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21