Arrays

Sep 11, 2011 at 5:48pm
can anyone please tell me how to compare elements in an array?
for example, i have {2, 3, 5, 2} and i want to compare if any of these four elements are equal or not.
(in this case there are two 2s, so there IS 'sameness')..it should now print "SIMILARITY"
how would i do that?
Sep 11, 2011 at 6:24pm
All you need to do is loop through the array and compare each value with the rest of the array.

This would need a double loop to accomplish but is a lot easier than it might sound.

Try the following program (written in Windows in Visual C++ 2010 but to make it run in Linux just remove the lines "#include <conio.h>" and "_getch();"):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <cstdlib>
#include <time.h>
#include <conio.h>
#include <iostream>
using namespace std;

int main(){
	printf("\n\n");

	printf("***************************\n");
	printf("** Array Comparison Test\n");
	printf("***************************");

	// This creates an array of random numbers for testing purposes
	int myArray[10];
	srand((int)time(0));
	for(int i = 0; i < 10; i++){
		myArray[i] = rand() % 10;
	}

	printf("\n\nArray contains:\n\t");
	for(int i = 0; i < 10; i++){
		printf(" %i", myArray[i]);
	}

	// This loop will find all UNIQUE pairs of duplicate numbers in the array.
	int duplicateCount = 0;
	printf("\n\nChecking for duplicate values...");
	for(int i = 0; i < 10; i++){
		for(int j = i; j < 10; j++){
			if(j != i){
				if(myArray[i] == myArray[j]){
					printf("\n\tSimilarity: the value '%i' was found at positions %i and %i.", myArray[i], i, j);
					duplicateCount++;
				}
			}
		}
	}

	printf("\n\nFinished. press enter to exit program.");
	_getch();
}
Last edited on Sep 11, 2011 at 6:25pm
Sep 11, 2011 at 8:27pm
@zippiking: I have 2 words of advise for you:

1. This is an imperfect world with imperfect people and some of the imperfect people just post in forums problems for other to solve; usually these problems are homework, meaning that answering them in full as you have done is feeding this terrible practice. I am not saying this particular poster is one of those; I am just saying that you should look out for this, unless you don't mind being everyone's personal assistant for free.

2. I'm not a big console programmer, but AFAIK, all functions in conio.h can be replaced by a more standard STL approach. For example, _getch() can be replaced by cin.get(). Also note that the code you present uses the old C function printf(), but we are in the C++ era. Why program in C when C++ is better? Just saying! :-P
Sep 11, 2011 at 9:03pm
I wouldn't say C++ is better then C! :O

They both have upside and downsides.
Sep 11, 2011 at 9:07pm
Really? Why would you say C++ appeared then? And it is an honest question, really. Because I always thought C++ appeared as a better, new iteration of the C language that added classes to it that allowed among other new things to ensure automatic cleanup of resources via class destructors and such. In what ways is C++ worse than C? I would be interested to know.
Sep 11, 2011 at 10:25pm
In C++, memory leaks are a problem. C is also faster and more lightweight. All supercomputers (Actual, 20 Billion $$ computers with 2000+ processors), ALL run Linux which is made in C.

But C++ has good old OOP, and amount other things, strong structure. 99% of AAA games are C++ with C at the side.

If you wanted a job in game programming (Or almost any other program position), they would love you to know C inside and out. For a lot of jobs, it's a must to know C as well as C++.

:)
Last edited on Sep 11, 2011 at 10:27pm
Sep 11, 2011 at 10:25pm
But like you said, using cout would be better then using fprint for the sheer new ways.
Sep 12, 2011 at 6:44am
Thanks for the heads up WebJose I guess you are probably right. I have been using printf() functions for so long I guess I am just used to them, and they work so well I find it hard to think of a reason to move from them :P Regardless from this point on I shall actively attempt to change my ways!

@Kong288, I personally don't think memory leaks are any more of a problem in C++ than they are in C. I would consider memory leaks to be a programmers error, not the languages =) Just my opinion.
Last edited on Sep 12, 2011 at 7:10am
Sep 12, 2011 at 9:47pm
Since C++ is a more advanced language with more things in it, it is very easy to have memory leaks. Seeing no one is perfect, you will have leaks, no matter how hard you try (At least for big projects).
Last edited on Sep 12, 2011 at 9:48pm
Topic archived. No new replies allowed.