If I have 2 arrays, say array1 = {2, 2, 4, 5, 6} which has sorted array elements and array2 = {4, 2, 6, 2, 5} that has same elements as array1 but not in sorted condition. How can I compare that both arrays have same elements. I have written the following code which works in all conditions except when array1 has two same elements and array2 has all different elements.
counter=0;
for (i=0; i<5; i++) {
for (int j=0; j<5; j++)
if (array2[i] == array1[j]) {
counter++;
array1[j]=0;
int result = 1; for (int j=0;j<N;j++) { bool found = false; for (int i=0;i<N && !found;i++) { if (a[j] == b[i]) found = true; } if (!found) return 0; }
I need to create a code that compares two in arrays without sorting them. They have to be the same length and contain the same elements in any order.
every integer in a[] is also in b[] every integer in b[] is also in a[] all such common values appear exactly the same number of times in both a[] and b[]
EX: a = {1, 2, 3}, b = {2, 3, 4} return 0 a = {1, 2, 3}; b = {2, 3, 1} return 1 a = {1, 2, 2}; b = {2, 2, 1} return 1 a = {1, 2, 2}; b = {2, 1, 1} return 0 a = {1, 1, 2, 2, 2}; b = {2, 1, 2, 1, 2} return 1
I'm looking to take in an array of less than 50 strings, and I want to find all of the unique words in that array (omitting the words that are repeated) and then outputting the unique words and unique word count. My code compiles, but my unique array is couting all of the words contained in the original array regardless of uniqueness.
#include <iostream> #include <string> #include <stdio.h> #include <string.h> using namespace std; int main() { cout << "Please type in some words." << endl; cout << "Type END and return when you are finished." << endl;
[code].....
This is what I get back.
You typed the following 14 words: red, green, blue, red, red, blue, green, blue, orange, red, reg, apple, banana, banana, END,
You typed the following 0 unique words: red, green, blue, red, red, blue, green, blue, orange, red, reg, apple, banana, banana, END
I'm not worried about the unique count yet, I just want to get the unique array containing the correct strings.
I've been working on an assignment that deals with newspaper advertisements. I have two arrays:
categoryCode (the code for the advertisement's category such as pets, cars, etc) and numWords (number of words in the ad).
The arrays are dynamic.
My assignment has the category listed each time there is an ad associated with it and then its number of words for that ad. If this were a real-world issue I would want my output to list each category only once despite how many ads were in it, list how many individual ads are in the category, and its total number of words for the ads in that category. When using parallel arrays, is there a way to do this?
Let's say I have categoryCode with three elements {5, 5, 7} and numWords {10, 12, 15}.
How would I make the numWords add together only when the categoryCode values are the same?
I would want the output to print: Category 5 Ads 2 Words 22 Category 7 Ads 1 Words 15
rather than how my instructor is having us do it like this: Category 5 Words 10 Category 5 Words 12 Category 7 Words 15
I think I'm getting hung up because my mind is confusing the element's location in the array with the element's actual value and I'm probably making this harder than it should be. How would I compare the categoryCode values in a dynamic array? If it were a fixed size I think I could just compare categoryCode[0] to categoryCode[1] and so on, right? But in this case I'm not sure how I would go about that.
I have created a new array and have wrote a code which will decrease || increase the value of the array element. I been trying to figure out how to set the value limit. For Example:
int[] stock = new int[] { 10, 10, 10, 10 };
How to set a limit on the elements that they will never go below to the negative integers and over 10?
I'm confused about accessing elements from arrays. Supposed I have an array of pointers to char:
char *names = { "John", "Rose", "Steven" };
To access one of the strings, should I use names[ 0 ][ i ], where i is an index in the set ( 0, 1, 2 ), or should I use names[ i ]? I would think it would be the first option, because this array has 1 dimension that contains others arrays, right?
So I'm trying to make two arrays, then the third array that will include common elements of those two arrays. I did make a code for that, but the problem is I don't know how to include that the elements do not repeat.
For example, if there are two arrays,
Elements of the first one: 2, 3
Elements of the second one: 2, 3, 2
The third array is going to be: 2 2
While I want it to be only 2.
#include <iostream> using namespace std; int* intersection(int* n1, int d1, int* n2, int d2, int& d3) { d3=0; for (int i=0; i<d1; i++) {
Supposing you have a 3 or more overlapping arrays (arrays having elements in common), and you wish to select 2 or more of the arrays with the maximum number of elements but less overlap as compared to the rest of the overlapping arrays.
Eg. A[4],B[6],C[5]. A+B contains 10 elements but say the overlapping element is 3, meaning it has 7 unique element.
Also B+C=11 elements , but supposing it has 5 overlaps, it would mean it has only 6 unique elements. A+B+C=15. Supposing the overlaps are 11 then it means the unique elements are 4. Ect. So per the example, the best array options with most unique element would be A+B .
I have a matrix that contains zero and nonzero elements. I want to do a function that return 3 arrays.
The first one is for nonzero elements the second array contains the corresponding row numbers of each nonzero element the third array contains the corresponding column numbers of each nonzero element.
My program will scan a message sent over Steam (An online game distributor ) and I want it to check if the Message contains one of the elements in the array. I want it to compare the String of the message to the values in the String array. I have tried looking into it but have unfortunately found nothing that would go along with my program outline. Here is my code so far:
if (add == true) { string[] itemNames = { "keys", "tickets", "nametags", "descriptiontags" }; string[] splitmessage = message.Split(' '); foreach (string word in splitmessage) { int strNumber;
[Code] .....
I am hoping for this to look like the picture of the code I wrote in the Attachments. [URL] .....
I'm a novice with C programming and i have to solve an error in the following code. The code works like you enter a password called "uoc" and it shows as OK. But surprisely when you entered another password as "Cambridge" it works fine too.
I think that the problem is in the array declaration but i'm checking resources and no success!
double x=1.00,y=2,z=4; if (y/z||++x) x+=y/z; printf("%f ",x); So (y/z||++x)
is true if at least one expression is true, that is either (y/z)!=0 or (++x)!=0 or both. I wonder how the comparison is done? Is (y/z) be truncated to integer or 0 be promoted to double?
I am trying to make a game where you have a secret code that is coded with colors like ROYG (red,orange,yellow,green) and I am having trouble when it tells you when you have a right color in the right spot or a right color in the wrong spot when you guess a color. How can I change my code under the function int comparearray where it will compare pointers to pointers and not integers and give me the correct number of "almost" and "correct".
I created a function to check whether the first character of a string is a number or not by comparing it to every base 10 digit using a for loop. If it is, the string is valid, if it is not, the string is not valid
Code: //ensure the first character is not a number int ValidFirstChar(char FirstChar) { int IsChar = TRUE, DigitCount = 0; /*boolean value indicating whether the first character is a number or not.*/
//check through all base 10 digits for (DigitCount = 0; DigitCount <= 9; DigitCount++) { if ((int)FirstChar == DigitCount)
[Code] ....
I have checked for the case where the first character is a number but it is displaying the error message for it. I have tried typecasting the char variable but that has not worked.
I'm trying to compare two float ranges and it seems to be a hit and miss. I'm trying to use a object origin and dimensions comparing it to another set for collisions.
I have trouble comparing two strings with strcmp. The bold part of the code are the parts that are not working and i hope somebody can explain to me what i did wrong. Goal of the code is to compare a city name with the name of already created cities and when two of them match to creat a Street between them. Depending on the street pointer different streets can be created
my vector containing all already created cities (it seem to work) std::vector<city*> citylist;//all cities
find city using strings and creating a road between them:
bool Map::add_street(Street *street, const string firsttown , const string secondtown) { city* hilfs1=Map::find_city(erste); city* hilfs2=Map::find_city( zweite); if (hilfs1==NULL || hilfs2==NULL) {return false ;} // Problem :both pointers are always NULL
[Code] ....
Here is also my implementation of the getname function :
i have 3 arrays total, 2 of them i am comparing to see if any of the values match at all, and then i am putting the value that matches into the 3rd array. currently i am trying
int isthere (int match[], int maxmatchLength, const int vec1[], int vec1Length, const int vec2[], int vec2Length) { for (i=0; i<vec1Length; i++) if vec1[i]==vec2[i]; vec1[i] = match[i]; }
But this will just match the same values are in the same spot. how do i fix it so that it compares one value in the first array to the whole second array, before going to the next number.
Lets say for example I have a BST that is sorted by char names, using strcmp. IF greater than 0 go right else go left.
I.E (this is just an example, they are not inserted correctly)
cat / dog buffalo / / fish mouse zebra snake
I wanted to make a copy of this BST IF the length of the nodes are greater than the root, how would I approach this? I kinda started on this but I'm not sure if I'm making this more difficult than it should be.
void BST::copygreater(node * root, node *& dest, int & holder) { if(!root) { dest = NULL; return; }
holder = strlen(root->name) + 1; //Don't know about this? If we do a recursive call then the value would change every call?
I have wrote the code below that compares to strings and sees if they are equals doesn't matter the order of the words . In the question we where asked not to use any library functions like the string functions in <string.h> and we have to do that all with pointers . I debugged my code and for some reason the first loop in the function keeps looping ...
#include<stdio.h> int IsEqual(char* str1,char* str2); #define SIZE 20 void main() { char str1[SIZE]="my name is monaya",str2[SIZE]="name monaya is my"; if (IsEqual(str1,str2))