Thread: Array Question

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    133

    Array Question

    I know this seems like a dumb question but it's been some time since I've worked with programming and I had a simple array question. I was wondering how I can fill this empty array with squared numbers corresponding to the original num_List array.

    Is this right at all?

    Also do the square and cubed functions not work with arrays?

    Code:
    // ***************************************
    //Doug Miller
    //Programming Assignment 1, Program 1
    //Square and cube examples
    //****************************************
    
    #include <iostream>
    #include <iomanip>
    #include <math.h>
    using namespace std;
    
    #define MAX 10
    
    int num_List [MAX] = 
    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
    int square_Nums[MAX] = {0};
    
    int cubed_Nums[MAX] = {0};
    
    using namespace std;
    
    int main()
    {
        //Declarations
    	
    	int i =0;
    	int squared_Term = 0;
    
    	for (i = 0; i < MAX;)
    	{
    		square_Nums[i] = (num_List[i] * num_List[i]); 
    		i++;
    	}
    
    	//Gather input
    
    	//Make calcuations
    
    	//Display output
    	
    	/* int p =0;
    
    	for (i = 0; i < MAX;)
    	{
            cout << square_Nums[p] << std::endl;
    		p++;
    	}
    	*/
    
        return 0;
    
    	std::cin.get();
    
    }//END OF MAIN

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The std::cin.get() is useless after the return statment, because it will never be executed. Your compiler would likely warn you about "unreachable code" if you enabled its warnings.

    There's no need to use global variables. Variables local to main() would work as well.

    Code:
    	for (i = 0; i < MAX;)
    	{
    		square_Nums[i] = (num_List[i] * num_List[i]); 
    		i++;
    	}
    Why not this?
    Code:
    	for (i = 0; i < MAX; i++)
    	{
    		square_Nums[i] = (num_List[i] * num_List[i]); 
    	}
    In C++, this
    Code:
    int square_Nums[MAX] = {0};
    is the same as
    Code:
    int square_Nums[MAX] = {};
    Your comments don't really make sense. The values are hard-coded into the program . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    I made the changes you suggested and now when I run the program I get a bunch of non sense for the output, and then the program eventually crashes! lol



    Code:
    // ***************************************
    //Doug Miller
    //Programming Assignment 1, Program 1
    //Square and cube examples
    //****************************************
    
    #include <iostream>
    #include <iomanip>
    #include <math.h>
    using namespace std;
    
    #define MAX 10
    
    using namespace std;
    
    int main()
    {
    	int num_List [MAX] = 
    	{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
    	int square_Nums[MAX] = {};
    
    	int cubed_Nums[MAX] = {};
    
    	int i =0;
    	int squared_Term = 0;
    
    	for (i = 0; i < MAX; i++)
    	{
    		square_Nums[i] = (num_List[i] * num_List[i]); 
    	}
    
    	//Display output
    	
    	int p =0;
    	for (i = 0; i < MAX; p++)
    	{
            cout << square_Nums[p] << std::endl;
    	}
    
        std::cin.get();
        
         return 0;
    
    }//END OF MAIN

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, you're bound to get garbage if you do this:
    Code:
    	int p =0;
    	for (i = 0; i < MAX; p++)
    	{
            cout << square_Nums[p] << std::endl;
    	}
    You're using i here, p there. Stick with one variable!
    Code:
    	for (i = 0; i < MAX; i++)
    	{
            cout << square_Nums[i] << std::endl;
    	}
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Thanks got it! Appreciate it!

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Oh, and one other thing I forgot to mention: you're not using anything from math.h, so unless you plan on using pow() or something, you can get rid of it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Yeah I took it out for the final run; it was in there because I was trying to use the sqrt and cubed functions but they didn't seem to be working with arrays.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's right, they only work with one number. But you can call them on your arrays, element by element.
    Code:
    	for (i = 0; i < MAX; i++)
    	{
    		square_Nums[i] = pow(num_List[i], 2.0); 
    	}
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM