bool sal_sk(int sal) // If sal is a composite figure, then true, if its not then false.
{
for (int i = 2; i <= sal; i++) {
if (sal%i == 0)
return true;
else
return false;
}
}
int lkd(int a, int b) // Checks the gcd
{
int c;
while (b > 0)
{
c = b;
b = a % b;
a = c;
}
return a;
}
int main()
{
int ok;
do
{
int n;//Number of elements
int*a; //Given number array variable
cout << "Put in the number of elements" << endl;
std::cin >> n;
a = new int[n];
cout << "Enter the array elements" << endl;
for (int i = 0; i < n; i++) {
std::cin >> *a;
}
int rez = a[0];
for (int i = 1; i < n; i++) {
if (sal_sk(a[i] == true))
rez = lkd(rez, a[i]);
}
delete[] a;
cout << "Composite figure gcd is " << rez << endl;
cout << " Do you want to continue(1) or to end (0)?" << endl;
cin >> ok;// Asks the user to enter in if he wants to continue or to end
} while (ok == 1);
}
Hey guys, I have two functions that check the greatest common divisor of composite figures in an array but it is random and idk why.
For Example if I enter 3 elements 4 6 9 it sometimes prints out that the gcd is 3 but sometimes it prints out that it is 1 which is correct but it happens at random times,and if I enter 9 4 6 it says that the gcd is 2, i just dont understand.Thanks in advance
bool sal_sk(int sal) returns true if sal is even false if it is not because you return always on the first iteration of your loop as #FrançoisAndrieux mentioned.
bool sal_sk(int sal) // If sal is a composite figure, then true, if its not then false.
{
for (int i = 2; i <= sal; i++) {
if (sal%i == 0)
return true;
else
return false;
}
}
To return true on composite make the following changes.
bool sal_sk(int sal) // If sal is a composite figure, then true, if its not then false.
{
for (int i = 2; i < sal; i++) {
if (sal%i == 0)
return true; // Has some factor other than 1 and itself
}
return false; // Does not have a factor other than 1 and itself
}
Another bug is here:
for (int i = 1; i < n; i++) {
if (sal_sk(a[i] == true))
rez = lkd(rez, a[i]);
}
This code passes true or false to sal_sk() depending on if a[i] !=0
Instead you want:
for (int i = 1; i < n; i++) {
if (sal_sk(a[i]))
rez = lkd(rez, a[i]);
}
This is also a bug:
cout << "Enter the array elements" << endl;
for (int i = 0; i < n; i++) {
std::cin >> *a; // This puts the value in a[0] always!
}
The code should be:
cout << "Enter the array elements" << endl;
for (int i = 0; i < n; i++) {
std::cin >> a[i]; // Put the value in the array at index i
}
The code here:
cout << "Enter the array elements" << endl;
for (int i = 0; i < n; i++) {
std::cin >> *a;
}
does not fill in all the array elements, but instead just overwrites the first entry.
This is far from the only problem with your code. In particular, sal_sk is not doing whatever you think its doing.
The system said: my code was time limit exceed. Is there away to shortcut my code?
I'm using vector to save nails to graph.
Input: e,n
Output: checkcase=1 => Check u adjacent with i
checkcase=2 => find nails that around u
#include <iostream>
#include <vector>
#include <list>
#include <string>
using namespace std;
int main()
{
int e, n;
string u, i;
//using vectors
vector<string> graph;
//use list
list<string>listElementsInCase2;
cin >> e;
cin >> n;
//loop for e
for (long index = 0; index < e; index++)
{
cin>>u>> i;
//add u to graph
graph.push_back(u);
//add i to graph
graph.push_back(i);
}
//Option
int checkCase;
long index;
//loop for n
while(sizeof(n))
{
cin >> checkCase;
if (checkCase == 1)
{
cin >> u >> i;
for (index = 0; index < 2 * e; index += 2)
{ //Check u adjacent ? i
if ((graph.at(index) == u) && (graph.at(index + 1) == i))
{
cout << "TRUE" << endl;
break;
}
}
if (index == 2 * e)
cout << "FALSE" << endl;
}
//checkCase=2
if (checkCase == 2)
{
cin >> u;
for (long index = 0; index < 2 * e; index += 2)
{
if (graph.at(index) == u)
listElementsInCase2.push_back(graph.at(index + 1));
}
// return 0
if (listElementsInCase2.empty() == true)
cout << "0";
else
{
while (0 < listElementsInCase2.size())
{ //find nails that around u
cout << listElementsInCase2.front();
listElementsInCase2.pop_front();
cout << " ";
}
}
cout << endl;
}
n--;
}
}
//end
You seem to have an infinite while loop in your code.
Your statement while(sizeof(n)) will never stop repeating the loop since sizeof(n) always returns the byte size of the type integer, which is always a positive number and thus always evaluates as true.
A shortcut for your code would be to replace the while loop with a for or while loop that actually ends at some point. Also sizeof is probably not the function you want.
for(int i=0; i<n; i++){ might be what you are looking for.
I need some help, I know this question was asked before but I don't get it and I cant solve it, so I need help. I need to move the elements of my array to a position to left. So if the input will be 1,2,3,4,5 then the output will be 2,3,4,5,1. I have done the same to right but to left I cant figure it out, please also explain the logic , thanks.
#include <iostream>
using namespace std;
int a[100],n,i,tempr,templ;
int main()
{
cin>>n;
for(i=1;i<=n;i++) cin >> a[i];
for(i=1;i<=n;i++)
{
tempr = a[n];
a[n] = a[i];
a[i] = tempr;
cout<<"Right: "<<a[i]<<endl;
}
for(i=1;i<=n;i++)
{
templ = a[2];
a[2] = a[i];
a[i] = templ;
cout<<"Left: "<<a[i]<<endl;
}
return 0;
}
Please help!
First problem is bad indexing:
for(i=1;i<=n;i++) cin >> a[i]; //wrong logic, C++ indexing start from 0
Correct approach:
for(i=0;i<n;i++) //all your loops
Second problem is wrong logic for shifting elements:
Corrected version:
//input example: 1 2 3 4 5
//to the left
int temp = a[0]; //remember first element
for(i=0;i<n-1;i++)
{
a[i] = a[i+1]; //move all element to the left except first one
}
a[n-1] = temp; //assign remembered value to last element
//output: 2 3 4 5 1
cout << "To left: " << endl;
for(i=0;i<n;i++)
cout << a[i] << endl;
//to the right
temp = a[n-1]; //remember last element
for(i=n-1;i>=0;i--)
{
a[i+1] = a[i]; //move all element to the right except last one
}
a[0] = temp; //assign remembered value to first element
//output: 1 2 3 4 5 because elements are shifted back by right shift
cout << "To right: " << endl;
for(i=0;i<n;i++)
cout << a[i] << endl;
EDIT:
How to display both shifts:
#include <iostream>
using namespace std;
int to_left[5], to_right[5],n,i,tempr,templ;
int main()
{
cout << "Input array size: ";
cin >> n;
for(i=0;i<n;i++)
{
cin >> to_left[i]; //read values to first array
to_right[i]=to_left[i]; //then copy values to second one
}
//shift first array to left
int temp = to_left[0];
for(i=0;i<n-1;i++)
{
to_left[i] = to_left[i+1]; //move all element to the left except first one
}
to_left[n-1] = temp; //assign remembered value to last element
//output: 2 3 4 5 1
cout << "To left: " << endl;
for(i=0;i<n;i++)
cout << to_left[i] << endl;
//shift second array to right
temp = to_right[n-1]; //remember last element
for(i=n-1;i>=0;i--)
{
to_right[i+1] = to_right[i]; //move all element to the right except last one
}
to_right[0] = temp; //assign remembered value to first element
//output: 1 2 3 4 5 because elements are shifted back by right shift
cout << "To right: " << endl;
for(i=0;i<n;i++)
cout << to_right[i] << endl;
return 0;
}
Note that your code look very much like C code. In C++, you can declare variables in any segment of code, not just at the beginning. In C++, you can declare variable in for loop like this: for(int i=0; i<...) - no need for global variable i
For reference, this would be good C++ code example that satisfies problem you are facing:
#include <iostream>
#include <vector>
int main()
{
std::size_t n; //size_t is unsiged type used for various sizes of containers or types
std::cout << "Input array size: ";
std::cin >> n;
std::vector<int> to_left(n), to_right(n); //two dynamic arrays containing integers, takin n as their size
for(std::size_t i=0;i<to_left.size();++i) //use vector size(), instead of n, also ++i in considered better for loops that i++ (may be faster)
{
std::cin >> to_left[i];
to_right[i]=to_left[i];
}
int temp = to_left[0]; //declare temp here, not at the begining of code
for(std::size_t i=0;i<n-1;++i)
to_left[i] = to_left[i+1];
to_left[n-1] = temp;
std::cout << "To left: " << std::endl;
for(std::size_t i=0;i<n;++i)
std::cout << to_left[i] << std::endl;
temp = to_right[n-1]; //reuse temp
for(int i=to_right.size()-1;i>=0;--i) //note int, not std::size_t, because size_t is always >=0, loop would never end.
to_right[i+1] = to_right[i];
to_right[0] = temp;
std::cout << "To right: " << std::endl;
for(std::size_t i=0;i<n;i++)
std::cout << to_right[i] << std::endl;
return 0;
}
And here would be ideal C++ code:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::size_t n;
std::cout << "Input array size: ";
std::cin >> n;
std::vector<int> to_left(n), to_right(n);
for(std::size_t i=0;i<to_left.size();++i)
{
std::cin >> to_left[i];
to_right[i]=to_left[i];
}
// rotate first array to the left
std::rotate(to_left.begin(), to_left.begin() + 1, to_left.end());
// rotate second array to right
std::rotate(to_right.rbegin(), to_right.rbegin() + 1, to_right.rend());
std::cout << "To left:" << std::endl;
for(auto x : to_left) //C++11 feature, x iterates through container
std::cout << x << std::endl;
std::cout << "To right:" << std::endl;
for(auto x : to_right)
std::cout << x << std::endl;
return 0;
}
Or you can use memmove(...) projected exactly for those purpose, here your sample:
#include <iostream>
#include <cstring>
using namespace std;
//rotate Left
void r_left(int *a,int n)
{
int tmp=a[0];
memmove(a,a+1,sizeof(int)*(n-1));
a[n-1]=tmp;
}
//rotate right
void r_right(int *a,int n)
{
int tmp=a[n-1];
memmove(a+1,a,sizeof(int)*(n-1));
a[0]=tmp;
}
void show(int *a,int n)
{
while(n--)
cout<<*a++<<' ';
cout<<endl;
}
int main()
{
int ar[]={1,2,3,4,5};
int n=sizeof(ar)/sizeof(ar[0]);
r_left(ar,n);
show(ar,n);
r_right(ar,n);
show(ar,n);
return 0;
}
easiest way to swap elements in C++ is to use std::iter_swap()
so for an array of 4 elements to swap elements 1 and 4 you would do the following
int a[4];
std::iter_swap(a, a+3);
note that you also need to #include <algorithm> for this to work
the basic logic of the function is that you give the location in memory of the 2 elements, so as the first element of an array is also its location in memory, you can pass a + n, when n is equal to the n-1 index number of the element you want to swap
As other already have stated it's all about indices. In a for-loop you are almost always in trouble if your stop condition is i <= size, because arrays in C++ are zero-indexed.
Where Black Moses alogrithm is far the easiest to understand (and probably the fastes), I read your code as if you try to swap the first value of the array through the array to the last position. Below I have tried to pin out this approach.
#include <stdio.h>
#include <tchar.h>
#include <iostream>
void ShiftLeft(int* pArr, size_t length)
{
for (size_t i = 1; i < length; i++)
{
int tmp = pArr[i - 1]; // Preserves the previous value
pArr[i - 1] = pArr[i]; // Overwrites the previous position with the current value
pArr[i] = tmp; // Stores the previous value in the current position
// All in all the first value is swapped down the array until it is at the length - 1 position
// and all the other values are swapped to the left.
/* For an array with 4 values the progression is as follows:
i = 0: 1 2 3 4
i = 1: 2 1 3 4
i = 2: 2 3 1 4
i = 3: 2 3 4 1
*/
}
}
void ShiftRight(int* pArr, size_t length)
{
for (size_t i = length - 1; i > 0; i--)
{
// This code does exactly the same as for ShiftLeft but the loop is running backwards
int tmp = pArr[i - 1];
pArr[i - 1] = pArr[i];
pArr[i] = tmp;
}
}
void Print(int* pArr, size_t length)
{
for (size_t i = 0; i < length; i++)
{
std::cout << pArr[i] << " ";
}
std::cout << std::endl;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
size_t length = sizeof(arr) / sizeof(arr[0]);
Print(arr, length);
ShiftLeft(arr, length);
Print(arr, length);
ShiftRight(arr, length);
Print(arr, length);
return 0;
}
#include <iostream>
using namespace std;
int a[100], outR[100], outL[100], n, i;
int main() {
cin >> n;
for (i = 0; i < n; i++) cin >> a[i];
// Right
for (i = 0; i < n; i++) {
outR[i+1]= a[i];
}
outR[0] = a[n-1]; // add first number
// Left
for (i = 1; i < n; i++) {
outL[i-1]= a[i];
}
outL[n-1] = a[0]; // add last number
// Answer
cout << "Right:\n";
for(i=0; i<n; i++) {
cout << outR[i] << endl;
}
cout << "Left:\n";
for(i = 0; i < n; i++) {
cout << outL[i] << endl;
}
return 0;
}
Simple answer where you can easily see everything, good luck.
You may be interested in ,,vector coding", it seems be easier if you spend some time on this:
#include <iostream>
#include <vector>
using namespace std;
vector <int> a, outR, outL;
size_t i;
int main () {
int n, temp_int;
cin >> n;
while (n--) {
cin >> temp_int; // here you read number to your vector
a.push_back(temp_int); // here you add this to vector
// remember that vector start from element 0 as like arrays
}
// Left
// remember that last element will be first
// you may have acces to size of your vector easily
for (i = 0; i < (a.size()-1); i++) {
outL.push_back(a.at(i+1)); // here you create new vector
}
outL.push_back(a.at(0)); // add last elemet which rotated
// Right
// to rotate left first you have push last element so
outR.push_back(a.at(a.size()-1)); // add first elemet which rotated
for (i = 1; i < a.size(); i++) {
outR.push_back(a.at(i-1)); // here you push rest
}
cout << "Left" << "\n";
for (i = 0; i < a.size(); i++) {
cout << outL.at(i) << endl; // here you print value
}
cout << "Right" << "\n";
for (i = 0; i < a.size(); i++) {
cout << outR.at(i) << endl; // here you print value
}
return 0;
}
int* leftShiftOneByOneWIthoutTemp(int arr[], int sz)
{
for (int i=0 ;i < sz-1; i++)
{
arr[i] = arr[sz-1] + arr[i];
arr[sz-1] = arr[i] - arr[sz-1] ;
arr[i] = arr[i] - arr[sz-1] ;
std::cout << "iter "<< i << std::endl;
printArray(arr,5);
}
std::cout << "final "<< std::endl;
printArray(arr,5);
return arr;
}
Replace your code (to shift array left) with below code.
templ = a[0];
for(i=0;i<n-1;i++)
{
a[i] = a[i+1];
cout<<"Left: "<<a[i]<<endl;
}
a[n-1] = templ;
cout<<"Left: "<<a[n-1]<<endl;
#include "../../../std_lib_facilities.h"
int main()
{
vector <int> nmb;
vector <int> rep;
vector <int> prt;
int flag = 0;
int temp = 0;
int br = 0;
int max = -1;
int ind = 0;
cout << "Enter as much integers as you like\n";
while (cin >> temp)
{
if (nmb.size() == 0)
{
nmb.push_back(temp);
prt.push_back(temp);
++rep[br];
++br;
}
else
{
for (int i = 0; i < nmb.size(); ++i)
{
if (temp == nmb[i])
{
++rep[i];
flag = 1;
}
}
if (flag == 0)
{
nmb.push_back(temp);
prt.push_back(temp);
++rep[br];
++br;
}
else if (flag == 1)
{
flag = 0;
prt.push_back(temp);
}
}
}
cout << "You've entered numbers\n";
for (int j = 0; j < prt.size(); ++j)
cout << prt[j] << " ";
for (int k = 0; k < rep.size(); ++k)
if (rep[k] > max)
{
max = rep[k];
ind = k;
}
cout << "\n\nMost repeated number is " << nmb[ind] << endl;}
My task is to write what number has been entered max times. I know it's probably not the best idea but it was the first "good" one I had so I went with it. It compiles fine but gives me that error from that title when running. I tried cout << in few places and it seems that problem starts at the beginning of while loop.
You try to access the first element of rep, which is an empty vector.
You have to actually add elements before you may access them. Right now you're reading from and writing to memory that is not yours.
i am trying to do simple sets intersection , it works correctly , but when output comes , it shows only memory address or garbage random value , help me ,i applied breakpoints , but it's not working . actually i am new in C++
#include<iostream>
using namespace std;
class set
{
private:
int size;
int *elem;
public:
set()
{
size = 0;
elem = NULL;
}
set(int s);
~set();
set(set &s);
set intersection(set A, int z);
void inputset();
void outputset();
};
set::set(int s)
{
size = s;
elem = new int[s];
}
void set::inputset()
{
int i;
cout << "Enter the set Element" << endl;
for(i = 0 ; i < size ; i++)
{
cin >> elem[i];
}
}
set set::intersection(set A, int z)
{
int i, j, k = 0;
set R(z);
for(i = 0; i < size; i++)
{
for(j = 0; j < A.size; j++)
{
if(elem[i] == A.elem[j])
{
R.elem[k] = A.elem[j];
k++;
break;
}
}
}
return R;
}
set::~set()
{
delete []elem;
}
set::set(set &s)
{
int i;
if(size > 0)
{
delete []elem;
}
size = s.size;
elem = new int[size];
for(i = 0; i < size; i++)
{
elem[i] = s.elem[i];
}
}
void set::outputset()
{
int i;
cout << "The elements of new set is : " << endl;
for(i = 0; i < size; i++)
{
cout << elem[i] << endl;
}
cout << endl;
}
int main()
{
int x, y, z;
char choice;
cout << "Enter sizeof set A" << endl;
cin >> x;
set S1(x);
S1.inputset();
S1.outputset();
cout << "Enter sizeof set B" << endl;
cin >> y;
set S2(y);
S2.inputset();
S2.outputset();
z = x + y;
set S3(z);
cout << "Enter I for intersection" << endl << "Enter U for union" << endl << "Enter D for difference" << endl;
cin >> choice;
switch(choice)
{
case'I':
S3 = S1.intersection(S2, z);
S3.outputset();
break;
default:
cout << "Invalid entry";
}
return 0;
}
There are several ways to check intersections between two sets, if your sets are in arrays, I'd suggest looping one of them and have a check if it's in between the other:
for (int i = 0; i < element1_size; i++) {
for (int u = 0; u < element2_size; u++) {
if (element1[i] == element2[u]) {
cout << "Intersection point : " << element1[i] << endl;
}
}
}
Your copy ctor should be set(const set &s); instead of set(set &s); What you have is a ctor that accepts a reference to set, which is not a copy ctor. So compiler generates default one for you and you have issue returning object by value from method intersection()
Also please fix your code indent, it is not easy to read it.
Also remove following lines from your "copy ctor":
if(size>0)
{
delete []elem;
}
You cannot delete that pointer, your object is not constructed yet.
How about using a map to keep track of the "intersected" elements in much more efficient way. Here is a snippet of what I have in mind:
std::map<int, int> m;
for(int i = 0; i < size; ++i)
m.insert( std::pair<int, int>(elem[i], 1) );
for(int i = 0; i < A.size; ++i)
if(m.count(A[i]) > 0)
R.elem[k++] = A[i]
This way you avoid the nested for-loops, and will be able to compute the intersection with logarithmic time complexity.