|
-
Mar 11th, 2001, 03:21 PM
#1
Thread Starter
Hyperactive Member
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
-
Mar 11th, 2001, 03:39 PM
#2
Frenzied Member
Use 2 for loops (one within the other) to loop through each dimension of the array.
Harry.
"From one thing, know ten thousand things."
-
Mar 11th, 2001, 03:46 PM
#3
Thread Starter
Hyperactive Member
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
-
Mar 11th, 2001, 04:16 PM
#4
Frenzied Member
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."
-
Mar 11th, 2001, 04:39 PM
#5
Thread Starter
Hyperactive Member
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
-
Mar 11th, 2001, 04:57 PM
#6
Frenzied Member
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;
}
-
Mar 11th, 2001, 05:13 PM
#7
Frenzied Member
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;
}
-
Mar 11th, 2001, 05:32 PM
#8
Frenzied Member
Well it looks like the question is answered then
Harry.
"From one thing, know ten thousand things."
-
Mar 11th, 2001, 05:37 PM
#9
Monday Morning Lunatic
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
-
Mar 11th, 2001, 05:58 PM
#10
Frenzied Member
Bitter, me? nahh, I'm listening to Slayer at the moment, how could I be bitter?
Harry.
"From one thing, know ten thousand things."
-
Mar 11th, 2001, 06:27 PM
#11
Thread Starter
Hyperactive Member
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
-
Mar 11th, 2001, 07:05 PM
#12
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|