Collatz sequence? - sequence

Im trying to solve the Collatz problem. It's all working except of one my int highest which should compare whether counter of one number is greater than counter of next number seems not to be working. I also tried my highest variable as array but still getting no result. Thanks for your any advice.
#include <vector>
#include <iostream>
using namespace std;
int main()
{
__int64 num;
int i;
int counter=0;
//vector<int> a(1000000);
//vector<int> b(1000000);
__int64 highest=0;
int j=0;
for(j=2;j<=1000000;j++)
{
num=j;
counter=0;
for(i=0;i<1000000;i++)
{
cout<<"num is: "<<j<<endl;
if(num==1)
{
break;
}
if(num%2==0)
{
num = num/2;
counter++;
cout<<num<<endl;
}
else if(num%2!=0)
{
counter++;
num= (num*3)+1;
cout<<num<<endl;
}
}
cout<<"counter: "<<counter<<endl;
//this part is not working
if(highest<=counter)
{
highest=counter;
cout<<"highest is: "<<highest<<endl;
}
}
return 0;
}

Related

What do the various portions of a compiler returned error mean?

The question is pretty basic.
Code:
#include<iostream>
#include<fstream>
using namespace std;
struct Customer
{
char name[20];
char cell[12];
double bal;
};
int get_total_records_from_file(char * filename)
{
// Implement this funtion
return 0;
}
void get_input_from_user(Customer * ptr){
cin.ignore();
for(int i = 0; i < customer_count; i++){
cout<<"Enter Customer Name (1-20 character long)"<<endl;
cin.getline(ptr[i].name,20);
cout<<"Enter Cell No. (11 character long)"<<endl;
cin.getline(ptr[i].cell,20);
cout<<"Enter Initial Balance"<<endl;
cin >> ptr[i].bal;
cin.ignore();
}
}
If I have this error:
main.cpp:22:21: error: ‘customer_count’ was not declared in this scope
what do the various portions of it mean? That is, what's 22? what's 21? etcetra.

OpenKinect acquire raw depth image

I am trying to use the example code from here.
I have made some changes in order to save the images to the computer. When I read the data in MATLAB it seems like values that should be 0 are set to 2047, and overall it does not seem to be correct when I reconstruct the 3D points using the default intrinsic camera parameters.
What I want to achieve is to save the images so that I can use
img = single(imread(depth.png'))/ 1000
and have the depth values in meters, and pixels with no measurements should be zero.
It is the Kinect V1 by the way.
Here is the code with comments where I have tried to change.
#include "libfreenect.hpp"
#include <iostream>
#include <vector>
#include <cmath>
#include <pthread.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
using namespace cv;
using namespace std;
class myMutex {
public:
myMutex() {
pthread_mutex_init( &m_mutex, NULL );
}
void lock() {
pthread_mutex_lock( &m_mutex );
}
void unlock() {
pthread_mutex_unlock( &m_mutex );
}
private:
pthread_mutex_t m_mutex;
};
// Should one use FREENECT_DEPTH_REGISTERED instead of FREENECT_DEPTH_11BIT?
class MyFreenectDevice : public Freenect::FreenectDevice {
public:
MyFreenectDevice(freenect_context *_ctx, int _index)
: Freenect::FreenectDevice(_ctx, _index), m_buffer_depth(FREENECT_DEPTH_11BIT),
m_buffer_rgb(FREENECT_VIDEO_RGB), m_gamma(2048), m_new_rgb_frame(false),
m_new_depth_frame(false), depthMat(Size(640,480),CV_16UC1),
rgbMat(Size(640,480), CV_8UC3, Scalar(0)),
ownMat(Size(640,480),CV_8UC3,Scalar(0)) {
for( unsigned int i = 0 ; i < 2048 ; i++) {
float v = i/2048.0;
v = std::pow(v, 3)* 6;
m_gamma[i] = v*6*256;
}
}
// Do not call directly even in child
void VideoCallback(void* _rgb, uint32_t timestamp) {
std::cout << "RGB callback" << std::endl;
m_rgb_mutex.lock();
uint8_t* rgb = static_cast<uint8_t*>(_rgb);
rgbMat.data = rgb;
m_new_rgb_frame = true;
m_rgb_mutex.unlock();
};
// Do not call directly even in child
void DepthCallback(void* _depth, uint32_t timestamp) {
std::cout << "Depth callback" << std::endl;
m_depth_mutex.lock();
uint16_t* depth = static_cast<uint16_t*>(_depth);
// Here I use memcpy instead so I can use uint16
// memcpy(depthMat.data,depth,depthMat.rows*depthMat.cols*sizeof(uint16_t));
depthMat.data = (uchar*) depth;
m_new_depth_frame = true;
m_depth_mutex.unlock();
}
bool getVideo(Mat& output) {
m_rgb_mutex.lock();
if(m_new_rgb_frame) {
cv::cvtColor(rgbMat, output, CV_RGB2BGR);
m_new_rgb_frame = false;
m_rgb_mutex.unlock();
return true;
} else {
m_rgb_mutex.unlock();
return false;
}
}
bool getDepth(Mat& output) {
m_depth_mutex.lock();
if(m_new_depth_frame) {
depthMat.copyTo(output);
m_new_depth_frame = false;
m_depth_mutex.unlock();
return true;
} else {
m_depth_mutex.unlock();
return false;
}
}
private:
// Should it be uint16_t instead or even higher?
std::vector<uint8_t> m_buffer_depth;
std::vector<uint8_t> m_buffer_rgb;
std::vector<uint16_t> m_gamma;
Mat depthMat;
Mat rgbMat;
Mat ownMat;
myMutex m_rgb_mutex;
myMutex m_depth_mutex;
bool m_new_rgb_frame;
bool m_new_depth_frame;
};
int main(int argc, char **argv) {
bool die(false);
string filename("snapshot");
string suffix(".png");
int i_snap(0),iter(0);
Mat depthMat(Size(640,480),CV_16UC1);
Mat depthf (Size(640,480),CV_8UC1);
Mat rgbMat(Size(640,480),CV_8UC3,Scalar(0));
Mat ownMat(Size(640,480),CV_8UC3,Scalar(0));
// The next two lines must be changed as Freenect::Freenect
// isn't a template but the method createDevice:
// Freenect::Freenect<MyFreenectDevice> freenect;
// MyFreenectDevice& device = freenect.createDevice(0);
// by these two lines:
Freenect::Freenect freenect;
MyFreenectDevice& device = freenect.createDevice<MyFreenectDevice>(0);
namedWindow("rgb",CV_WINDOW_AUTOSIZE);
namedWindow("depth",CV_WINDOW_AUTOSIZE);
device.startVideo();
device.startDepth();
while (!die) {
device.getVideo(rgbMat);
device.getDepth(depthMat);
// Here I save the depth images
std::ostringstream file;
file << filename << i_snap << suffix;
cv::imwrite(file.str(),depthMat);
cv::imshow("rgb", rgbMat);
depthMat.convertTo(depthf, CV_8UC1, 255.0/2048.0);
cv::imshow("depth",depthf);
if(iter >= 1000) break;
iter++;
}
device.stopVideo();
device.stopDepth();
return 0;
}
Thanks in advance!
Erik
I dont have any experience with OpenKinect in particular; but should your depth buffer be uint16?
std::vector<uint8_t> m_buffer_depth;
Also; for Matlab, do check if the image that you are reading is a uint16 or uint8. If its the latter then convert it to uint16
uint16(imread('depth.png'));
Sorry couldn't help more. Hope this helps.
The values you have are the raw depth values. You need to remap those into MM for the numbers to make sense. Kinect 1 can see up to 10 meters. So I would go with raw_values/2407*10000.
If the values are saturated at 2047, you are probably using the FREENECT_DEPTH_11BIT_PACKED depth format.
For work in Matlab, it is always easier to use FREENECT_DEPTH_MM or FREENECT_DEPTH_REGISTERED.
Enjoy.

Bug in the program for finding two strings are anagram or not

i wrote the program to find two strings are anagram of each other or not. here is the code
#include<iostream>
using namespace std;
int main()
{
char a[4]={'a','r','m','y'};
char b[4]={'m','a','r','y'};
int sizea=sizeof(a)/sizeof(char);
int sizeb=sizeof(b)/sizeof(char);
int hit=0;
if(sizea!=sizeb)
{
cout<<"size not equal;";
}
else
{
for(int i=0;i<sizea;i++)
{
for(int j=0;j<sizeb;j++)
{
if(a[i]==b[j])
{
cout<<'\n'<<"match found"<<a[i];
hit=hit+1;
break;
}
if(hit==0)
{
cout<<'\n'<<"not anagram";
}
}
}
}
return 0;
}
but when i'm running it it always shows the output as-
not anagram
match found a
match found r
match found m
match found y
why is it showing "not anagram even when i incremented the hit value?

sudoku algorithm analysis

This is the code for solving sudoku (9x9):
#include <iostream>
#define dim 9
#define br_polja dim*dim
using namespace std;
bool Fsearch (int tab[dim][dim],int *x,int *y, int cand[dim], int *num_cand, int &free) {
int min=9;
*x=-1;
*y=-1;
free=0;
for (int i=0;i<dim;i++) {
for (int j=0;j<dim;j++) {
if (tab[i][j]!=0) continue;
free+=1;
*num_cand=0;
int cand_f[dim];
for (int w=0;w<dim;w++)
cand_f[w]=1;
int p,q;
p=i+3-(i%3);
q=j+3-(j%3);
for (int w=p-3;w<p;w++)
for (int r=q-3;r<q;r++)
if (tab[w][r]!=0){
cand_f[tab[w][r]-1]=0;
}
for (int w=0;w<dim;w++) {
if (tab[w][j]!=0) cand_f[tab[w][j]-1]=0;
if (tab[i][w]!=0) cand_f[tab[i][w]-1]=0;
}
for (int w=0;w<dim;w++)
if (cand_f[w]!=0)
*num_cand+=1;
if (*num_cand==1) {
*x=i;
*y=j;
int z=0;
for (int w=0;w<dim;w++)
if (cand_f[w]!=0) {
cand[z]=w+1;
z++;
}
return true;
}
else if (*num_cand<min && *num_cand>0) {
int z=0;
for (int w=0;w<dim;w++)
if (cand_f[w]!=0) {
cand[z]=w+1;
z++;
}
min=*num_cand;
*x=i;
*y=j;
}
else if (*num_cand==0) {
return false;
}
}
}
*num_cand=min;
if (free<1) return false;
else return true;
}
bool Fsudoku(int tab[dim][dim]) {
int free=0;
int x,y;
int cand[dim];
int num_cand;
brojacK=0;
if (!Fsearch(tab,&x,&y,cand, &num_cand,free)) {
if (free==0) return true;
else return false;
}
for (int i=0;i<num_cand;i++) {
tab[x][y]=cand[i];
if (Fsudoku(tab)) return true;
else tab[x][y]=0;
}
return false;
}
Can someone tell help me classify this algorithm in Big O or Big Theta notation. I got O(n^3) but as we can see that's not true! So, give me a tip or an advice how to start, I would be very thankful.
And if you like, I can post this in pseudo-code too!
Thanks,
MB
Big O is about which terms dominate.
So how many times do each of the loops run?
Are they of fixed length, or do they vary?
What parameters can change that would effect the run time?

variable argument function

while doing a program related to variable argument function i
got the header file stdarg.h and have done some simple problem using it
but now when i a changing the actual argument's type it is showing some weird behaviour
here is my code:
#include<stdio.h>
#include<stdarg.h>
void fun(int a,...)
{
va_list k;
va_start(k,a);
int i=0;
printf("%d ",a);
while((i=va_arg(k,int)!=0)
{
printf(" %d ",i);
}
va_end(k);
}
int main()
{
fun(1,2,3,4,5,6);
printf("\n");
fflush(); //and without flush it is also showing some extra garbage value
fun(2,4,5);
printf("\n");
fflush();
fun('c','f','g','l');
return 0;
}
If you use the integer value 0 to indicate end of the argument list, you should also pass a 0 to fun.
fun(1,2,3,4,5,6,0);
fun(2,4,5,0);
First, end your list with 0, because you check for !=0 to detect the end. And also:
while((i=va_arg(k,int))!=0)
instead of
while(i=va_arg(k,int)!=0)
!= has higher precedence than =
This will give you the expected output:
1 2 3 4 5 6
Here's the complete code:
#include<stdio.h>
#include<stdarg.h>
void fun(int a,...)
{
va_list k;
va_start(k,a);
int i=0;
printf("%d ",a);
while((i=va_arg(k,int))!=0)
{
printf(" %d ",i);
}
va_end(k);
}
int main()
{
fun(1,2,3,4,5,6,0);
printf("\n");
//fun(2,4,5,0);
printf("\n");
//fun('c','f','g','l','\0');
getch();
return 0;
}