Results 1 to 12 of 12

Thread: Beginner at C++ Stuck on Homework

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Gig Harbor, WA; Posts: 89950
    Posts
    360
    OK I have a 2dimensional array. I need to be able to search through the array and tell the user how many times each number appears in the array. My hack job code is as follows:


    Code:
    //this program counts the number of times A number occurs In an array
    
    #include <iostream.h>
    
    void main()
    {
    	short numbers[5][3] =  {{2, 3, 5}, 
    							{6, 3, 5}, 
    							{7, 2, 1}, 
    							{4, 5, 9}, 
    							{3, 5, 1}};
     
    	
    	short tmpNum = 1; //Number being seeked will increase by 1 Until done
    	short tmpCount[] = {0}; //Number of times it has been found (counter)
    	short xCt = 0;
    
    
    
    	for(tmpNum = 1; tmpNum <10; tmpNum  = tmpNum +1) //Each Number		
    //I need A way To look into the array For Each number from 1-9
    
    
    
    
    
    //Display Results
    
     cout << "Number " << tmpNum << " appears " << tmpCount[xCt] << " times. " << endl;
    
    
    } //end of main Function
    
    'Code improved by vBulletin Tool (Save as...)

    Please Help, I'm way confused!
    Mahalo
    VB6(SP5), VC++, COBOL, Basic, JAVA
    MBA, MCSD, MCSE, A+
    Computer Forensics

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Use 2 for loops (one within the other) to loop through each dimension of the array.
    Harry.

    "From one thing, know ten thousand things."

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Gig Harbor, WA; Posts: 89950
    Posts
    360
    Sorry, I am REAL NEW....Can you give me a push in the right direction?
    Mahalo
    VB6(SP5), VC++, COBOL, Basic, JAVA
    MBA, MCSD, MCSE, A+
    Computer Forensics

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Well err... hmm... I really don't know how to make it clearer without just doing the whole thing...

    You know how to do VB right? I get the impression from your profile and the fact that you have 120 posts yet you rarely post in the C++ forum that you must do. Well how would you do it in VB? It's not a difficult problem, and I don't want to just give you the answer because that's not the point. If you're having problems converting VB code to C++ then I can help you with syntax and the like. Why don't you write it in VB first and then convert it?
    Harry.

    "From one thing, know ten thousand things."

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Gig Harbor, WA; Posts: 89950
    Posts
    360
    Alright how's this:

    dim number(5,3)
    dim Find as integer
    dim foundCount as integer
    dim Y as Integer, X as Integer

    find = 1
    do until find = 10
    call Go See Y
    find = find + 1
    Loop


    ______________________________________
    Frivate Function Go See Y()
    Do until X = 3

    If number(Y,X) = Find then
    findCount = FindCount +1
    Y = Y +1
    End if
    If Y = 5 then
    x = x + 1
    Y = 1
    end if
    Loop
    End Sub
    __________________________________________

    OK now let's have it!
    Mahalo
    VB6(SP5), VC++, COBOL, Basic, JAVA
    MBA, MCSD, MCSE, A+
    Computer Forensics

  6. #6
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Something like this maybe:
    Code:
    #include <iostream.h>
    
    int main(int argc, char* argv[])
    {
    	short numbers[5][3] =  {{2, 3, 5}, 
    							{6, 3, 5}, 
    							{7, 2, 1}, 
    							{4, 5, 9}, 
    							{3, 5, 1}};
    int count = 0; //counter
    for(int i=0;i<5;i++)
    {
    	 for(int y=0;y<3;y++)
    	 {
    		 if (numbers[i][y] == 3 //the number that is searched in the array) 
    			count++;
    	 }
    }
    cout<<count<<endl;
    	return 0;
    }
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  7. #7
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Here is the code you need:
    Code:
    #include <iostream.h>
    
    int main(int argc, char* argv[])
    {
    	short numbers[5][3] =  {{2, 3, 5}, 
    							{6, 3, 5}, 
    							{7, 2, 1}, 
    							{4, 5, 9}, 
    							{3, 5, 1}};
    int count[9]; 
    
    for(int nro = 0;nro<10;nro++)
    {
    	count[nro] = 0;
    	for(int i=0;i<5;i++)
    	{
    		for(int y=0;y<3;y++)
    		{		
    			
    			if (numbers[i][y] == nro) 
    			{
    				++count[nro];
    			}
    		}
    	}
    }
    
    for(int i = 0;i<10;i++)
    cout<<i<<"--"<<count[i]<<endl;
    //prints info how many times each number appears in the array
    	return 0;
    }
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  8. #8
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Well it looks like the question is answered then
    Harry.

    "From one thing, know ten thousand things."

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Ooh...Harry looks bitter
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Bitter, me? nahh, I'm listening to Slayer at the moment, how could I be bitter?
    Harry.

    "From one thing, know ten thousand things."

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Gig Harbor, WA; Posts: 89950
    Posts
    360
    Thanks guys

    I don't know why C++ confuses the daylights out of me.

    But it does.


    Is Slayer still around?
    Mahalo
    VB6(SP5), VC++, COBOL, Basic, JAVA
    MBA, MCSD, MCSE, A+
    Computer Forensics

  12. #12
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Yep, still alive and very much kicking They toured with Sepultura and System of a Down fairly recently I think... like within the last year or two.
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width