I'm working on a program that will display all possible permutations for an array, and then store the unique permutations in another array, however I am having issues with storing the unique permutations. I was going through my code and was getting a few errors because I had created the uniquePermutations variable and hadn't initialized. After trying to access the variable the program would crash so then I tried setting it equal to nullptr which helped.
So now when I use my copyUniquePermutations function (which is called in the permute function, it checks to see if the array is empty with nullptr and then if it is, we declare 3 new arrays, and set each spot equal to NULL so that we won't get any undefined behavior. Then next, I check to see if any spots are NULL so that we won't get to the equalArrays function which may cause issues and then we get to the assignment part which causes issues. Since we assigned newArray[i] to be NULL why does the computer say that it is having issues writing to this spot?
#include <iostream>
using namespace std;
int permutations[] = { 2, 1, 2 };
void swap(int &x, int &y)
{
int temp;
temp = x;
x = y;
y = temp;
}
bool equalArrays(int array1[], int array2[], int size)
{
for (int i = 0; i < size; i++)
if (array1[i] != array2[i]) return false;
return true;
}
void copyUniquePermutations(int oldArray[], int *newArray[])//This is the function that is causing issues
{
for (int i = 0; i < 3; i++)
{
if (newArray == nullptr)
{
newArray = new int*[3];
for (int j = 0; j<3; j++)
newArray[i] == NULL;
}
if (newArray[i] == NULL || !equalArrays(oldArray, newArray[i], 3))
{
for (int j = 0; j < 3; j++)
newArray[i][j] == oldArray[j];
}
}
}
void permute(int permutations[], int *uniquePermutations[], int l, int r)
{
int i;
if (l == r)
copyUniquePermutations(permutations, uniquePermutations);
else
{
for (i = l; i <= r; i++)
{
swap((permutations[l]), (permutations[i]));
permute(permutations, uniquePermutations, l + 1, r);
swap((permutations[l]), (permutations[i]));
}
}
}
int main()
{
int **uniquePermutations = nullptr;
permute(permutations, uniquePermutations, 0, 2);
for (int i = 0; i < 3 ; i++)
delete[] uniquePermutations[i];
delete[] uniquePermutations;
return 0;
}
I think here you need to have j in newArray[j]
for (int j = 0; j<3; j++)
newArray[i] == NULL;
Related
Question: Find unique elements from the given array and print them
I have dry run below given code many times but i am not getting what is the problem in the code.
#include <iostream>
using namespace std;
void findingUniqueElement(int array[], int size)
{
int brray[100];
int count = 0;
bool equal = 0;
for (int i = 0; i < size; i++)
{
equal = 0;
int value = array[i];
for (int j = 0; j < size; j++)
{
if (i != j && value == array[j])
{
equal = 1;
break;
}
}
if (equal == 0)
{
brray[i] = value;
count++;
}
}
for (int i = 0; i < count; i++)
{
cout << brray[i] << " ";
}
}
int main()
{
int arr[6] = {1, 2, 2, 5, 3, 7};
findingUniqueElement(arr, 6);
}
i was expecting as an output
1 5 3 7
but when i run the code, getting as output
1 1877357483 1878039440 5
Welcome to SO! Here is the code modified by me:
#include <cstring>
#include <iostream>
using namespace std;
void findingUniqueElement(int array[], int size) {
int brray[100];
memset(brray, 0, 100); // modified here
int count = 0;
bool equal = 0;
for (int i = 0; i < size; i++) {
equal = 0;
int value = array[i];
for (int j = 0; j < size; j++) {
if (i != j && value == array[j]) {
equal = 1;
break;
}
}
if (equal == 0) {
brray[i] = value;
count++;
}
}
for (int i = 0; i < size; i++) { // modified here
if (brray[i] != 0) // modified here
cout << brray[i] << " ";
}
}
int main() {
int arr[6] = {1, 2, 2, 5, 3, 7};
findingUniqueElement(arr, 6);
}
I modified three places in your code:
You should initialize brray when you declare it, the strange values output are because they were in the memory when you defined brray, you should clean the memory before you use it.
The way you store unique number in brray is not very correct, when array[i] is not unique, you will add i, which leave 0 at brray[i], so to fit your code, you should make i from 0 to size. To get more clear about what I'm talking about, you can check the memory of brray.
Since I assume there is no 0 in your test data, so if there is a 0 in barray, you know it is not a valid value, just skip it. Also, you can use a more elegant way to do this, like #Anand Sowmithiran commented:
int brray[100];
memset(brray, 0, 100);
int count = 0;
for (/* conditions... */) {
// if there is a unique number
brray[count++] = unique_number;
}
for (int index = 0; index < count; index++) {
cout << brray[index] << " ";
}
Hope my answer is helpful!
I have got this error: "error: request for member 'nume' in 'tablou[j]', which is of non-class type ' [100]'", and I don't really know how to solve it.I tried searching on youtube and google but I found nothing .Does anyone have any ideas for how to solve this?
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct{
int counter;
char nume[20] = " ";
}tablou[10][100];
int main()
{
int n, counter = 0;
char second[10][100];
bool verify = true;
cout<<"Cate nume?";
cin>>n;
for(int i = 0; i <= n; i++)
{
cin.getline(second[i],20);
}
for(int i = 0; i <= n; i++)
{
verify = true;
for(int j = 0; j < i; j++)
{
if(strcmp(second[i], tablou[j].nume) == 0)
{
verify = false;
}
}
if(verify == true)
{
strcpy(tablou[i].nume, second[i]);
for(int k = 0; k < n; k++)
{
if(strcmp(tablou[i].nume, second[k]))
{
tablou[i].counter++;
}
}
}
}
for(int i = 0; i <= n; i++)
{
cout<<tablou[i].nume<<" "<<tablou[i].counter<<endl;
}
return 0;
}
tablou is a 2d array
struct{
int counter;
char nume[20] = " ";
}tablou[10][100];
its elements are tablou[x][y]
you try to access an element with only one index
if(strcmp(second[i], tablou[j].nume) == 0)
----------------------------^
I do not know what your code is trying to do , but thats why you get the error
The array tablou is a two-dimensopnal array
struct{
int counter;
char nume[20] = " ";
}tablou[10][100];
So for example the expression tablou[j] has the array type the_unnamed_structure[100].
So such an expression like this
tablou[j].nume
is incorrect and the compiler issues an error.
Maybe actually you mean the following declaration of the array
struct{
int counter;
char nume[20] = " ";
}tablou[10];
Also in these loops
for(int i = 0; i <= n; i++)
{
verify = true;
for(int j = 0; j < i; j++)
{
if(strcmp(second[i], tablou[j].nume) == 0)
{
verify = false;
}
}
if(verify == true)
{
strcpy(tablou[i].nume, second[i]);
for(int k = 0; k < n; k++)
{
if(strcmp(tablou[i].nume, second[k]))
{
tablou[i].counter++;
}
}
}
}
some elements of the array tablou can be skipped if verify is set to false because you are using the index i to assign elements of the array tablou. That is the number of actual elements of the array tablou can be less than n. In this case this for loop
for(int i = 0; i <= n; i++)
{
cout<<tablou[i].nume<<" "<<tablou[i].counter<<endl;
}
will invoke undefined behavior because the data member counter will be uninitialized for some outputted elements of the array.
You need to support a separate variable as an index in the array tablou.
I have three functions below, I'm not sure why the second and the third one have a warning at *arr but the first one doesn't. What does the warning mean and how to fix this?
IDE: Clion 2017.3 MinGW64 5.0, CMake 3.9.4
Thank you.
int getFirstEven(int n, int *arr) {
for (int i = 0; i < n; ++i) {
if (arr[i] % 2 == 0)
return arr[i];
}
return -1;
}
int getLastOdd(int n, int *arr) {
int lastOdd = -1;
for (int i = 0; i < n; ++i) {
if (arr[i] % 2 != 0)
lastOdd = arr[i];
}
return lastOdd;
}
int countElement(int n, int *arr, int e) {
int cnt = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == e)
cnt++;
}
return cnt;
}
It makes sense to favor immutability where possible and to indicate immutable things with const.
The warning means that your function does not modify the data pointed at by arr and so the function could be better declared with pointer to const parameter. About like that:
int getFirstEven(int n, int const* arr) {
for (int i = 0; i < n; ++i) {
if (arr[i] % 2 == 0)
return arr[i];
}
return -1;
}
I'm writing a program that is made of a function that takes in a 2d array, c0unts the evens in the array and returns the amount of evens in that array. The function isn't returning the value that i intend it to, which is 6. Sometimes I get 0, sometimes I get a number like 2147483646.
#include <iostream>
#include <array>
using namespace std;
const int MaxNumOfRows = 3;
const int MaxNumOfColumns = 2;
int Even(int A[MaxNumOfRows][MaxNumOfColumns], int length, int width)
{
int NumnberOfEvens = 0;
int i;
int j;
for (i = 0; i < length; length++)
{
for (j = 0; j < width; width++)
{
if (A[i][j] % 2 == 0)
{
NumnberOfEvens++;
}
}
}
return NumnberOfEvens;
}
int main()
{
//int output = 0;
int A[MaxNumOfRows][MaxNumOfColumns] =
{
{2,2},{2,4},{2,2}
};
Even(A, MaxNumOfRows, MaxNumOfColumns);
//output = Even(A, MaxNumOfRows, MaxNumOfColumns);
cout << Even(A, MaxNumOfRows, MaxNumOfColumns) << endl;
system("pause");
return 0;
}
Check those for loops, I imagine you want to be incrementing the variables ++i and ++j rather than width++ and length++.
With an example this trivial, I imagine stepping through the executing code and finding the problem in a debugger would be pretty straightforward...are you writing this using an IDE with a debugger?
You are not applying increment on loop variables ('i' and 'j') here. 'length' and 'width' are increasing (due to length++, width++) whereas 'i' and 'j' are not. So, loop won't stop and thus the garbage values.
for (i = 0; i < length; length++)
{
for (j = 0; j < width; width++)
{
if (A[i][j] % 2 == 0)
{
NumnberOfEvens++;
}
}
}
This must work.
for (i = 0; i < length; i++)
{
for (j = 0; j < width; j++)
{
if (A[i][j] % 2 == 0)
{
NumnberOfEvens++;
}
}
}
I have the following function to find the parent array in order to obtain a minimum spanning tree of a graph using Prim's algorithm.
#include<stdlib.h>
#include <limits.h>
#include <iostream>
int printMST(int parent[], int n, int** graph)
{
for (int i = 1; i < n; i++)
std::cout<<parent[i]<<" - "<<i<<" "<<graph[i][parent[i]]<<"\n";
}
int* prim(int** graph,int no_of_vertices);
int main(){
int no_of_vertices;
std::cin>>no_of_vertices;
int** graph = new int*[no_of_vertices];
for(int i = 0; i < no_of_vertices; ++i)
graph[i] = new int[no_of_vertices];
for(int i = 0; i <no_of_vertices; ++i)
for(int j = 0; j < no_of_vertices; ++j)
std::cin>>graph[i][j];
int* parent;
parent= prim(graph,no_of_vertices);
// Print the solution
printMST(parent, no_of_vertices, graph);
return 0;
}
int nodeWithMinKey(int key[],bool mst[], int no_of_vertices)
{
int min=1000,min_index;
for(int i=0;i<no_of_vertices;i++)
{
if(mst[i]=false && key[i]<min)
{
min=key[i];
min_index=i;
}
}
return min_index;
}
int* prim(int** graph, int no_of_vertices){
int* parent = (int*)malloc(sizeof(int)*no_of_vertices);
int key[100];
bool mst[100];
int i;
for(i = 0; i<no_of_vertices; i++)
{
key[i] = 1000;
mst[i] = false;
}
key[0] = 0;
parent[0] = -1;
for(i = 0; i<no_of_vertices-1; i++)
{
int u = nodeWithMinKey(key, mst, no_of_vertices);
mst[u] = true;
for(int v = 0; v<no_of_vertices; v++)
{
if(graph[u][v] && mst[v] == false && graph[u][v] < key[v])
{
parent[v] = u;
key[v] = graph[u][v];
}
}
}
return parent;
}
But the parent array is having all the values as '0'(zero), don't know where the condition has gone wrong.
This line
if(mst[i]=false && key[i]<min)
has an assignment = instead of a comparison ==. This will always cause the if test to always fail, so your min_index will never be set.