daviddoria 334 Posting Virtuoso Featured Poster

It looks like you never set grid[row][column] to true in the uncommented portion of the code.

Also, can you explain the expected output? Now (maybe related to my last statement), I see nothing output (after commenting the "test first loop" type statements. This leads me to believe that grid[j] is never true.

Also, please please please don't use global variables (grid here is global).

daviddoria 334 Posting Virtuoso Featured Poster

You can do

for (int x = 0; x < counter; x++)
	{
		double score = scores[x];
		if (x < 0)
		{
		}
		if ((score >= 0)&&(score > high))
			high = score;
		if ((score >= 0)&&(score < low))
			low = score;
		else
                        break;
	}

cout << "Average is " << (total / (double) (counter)) << endl;
cout << "Highest is " << high << endl; 
cout << "Lowest is " << low << endl;

Also, setting low = 75.0; is not a safe assumption. What if all of the scores are above 75?

David

daviddoria 334 Posting Virtuoso Featured Poster

Please mark the thread as solved.

daviddoria 334 Posting Virtuoso Featured Poster

This is a pretty standard "first assignment". Please show us that you have tried. You won't get any help until you post some code and show us where you are stuck.

David

daviddoria 334 Posting Virtuoso Featured Poster

Here are some observations

1) You should definitely use an std::vector<double> instead of a fixed length array. There is no reason to arbitrarily restrict the user to 75 inputs. If you don't change this, you should certainly check if the counter goes above 75 and throw an error if it does.

2) You are starting y at 0 and only incrementing it, so it will never be zero, so you don't have to check if(y>=0) 3) You should not store the "-1 to stop" in the array. You should get the value from the user and only put it in the array if it is > 0.

4) x will never be less than zero.

5) If you do #3, 'score' will never be < 0.

6) Your actual problem is that you are doing the output inside the for loop. You should move the output to the very end of the program (outside of the loop).

Hope that helps,

David

daviddoria 334 Posting Virtuoso Featured Poster

You had me pretty confused on this one. It seems like qApp is a macro or something (reserved word).

I changed it to:

QApplication a(argc, argv);
    
return a.exec();

and then it worked fine.

David

daviddoria 334 Posting Virtuoso Featured Poster

Here is the fix for your error - you have to dereference the value:

if(*(CGraf::matr_adi[C[p],i])==1)

But yikes, you really really really should consider using more descriptive variable names, and COMMENT COMMENT COMMENT!

There is some personal preference involved, but here is what I would change:

matr_adi -> AdjacencyMatrix
nr_vertices -> NumberOfVertices
BF -> BreadthFirst
det_conex -> HasConnectedComponents
void cycles() -> bool HasCycle()
void paths() -> void paths(int nodeA, int nodeB, int &shortestPathLength, int &longestPathLength)
xp -> ??
u -> ??
p -> ??

See what I mean?

You want someone who reads your code to have as easy of a time understanding what is going on as possible.

Good luck,

David

daviddoria 334 Posting Virtuoso Featured Poster

You will just be able to adapt. You certainly will not have to start over. The things that will change you will probably not even learn about until several years after starting with c++ :)

daviddoria 334 Posting Virtuoso Featured Poster

Definitely no need to wait. 0x will just make some things easier for you, but probably not until you've got at least a couple of years of experience with the "old" c++.

Definitely continue with your book for the concepts, but there are some good examples of how to do specific things here:
http://programmingexamples.net/index.php?title=CPP

Good luck,

David

daviddoria 334 Posting Virtuoso Featured Poster

Please start a new thread when you have a new question. What you have done here (posting a new question at the bottom of an existing thread) is called "thread hijacking" and is very frowned upon :(

daviddoria 334 Posting Virtuoso Featured Poster

Look at #2 in my last post. You did not change this.

parth27987 commented: Helped a lot! Soleved the problem. +1
daviddoria 334 Posting Virtuoso Featured Poster

1) Line 56:

return (symbol);

you can't return something in a void function.

2) Your ambiguous function is because you declare it like:

void func1(int rows,int column,int blocks,char symbol);

and define it like

void func1(int &rows,int &column,int &blocks,char &symbol)

(Note that one has &'s and the other doesn't). These must match exactly.

3) You should always use more descriptive function names. Func1 and func2 doesn't really help anyone know what is going on :)

David

daviddoria 334 Posting Virtuoso Featured Poster

What is the error? What is the expected output? What is the current output?

daviddoria 334 Posting Virtuoso Featured Poster

What is the output supposed to be? What is the current output?

daviddoria 334 Posting Virtuoso Featured Poster

I'm assuming this is for a course (if it is not, use a library (VNL, Eigen, etc)). It if is, I'd imagine you need a Matrix class and overload the * operator for it. Internally you could store an array[9] or an array[3][3].

daviddoria 334 Posting Virtuoso Featured Poster

Look up "iostream", "cin", "conditionals" ("if"), and "mod" ("%").

David

daviddoria 334 Posting Virtuoso Featured Poster

You could use Qt. It is nice and also cross platform :)

Here are some examples:
http://programmingexamples.net/index.php?title=Qt

daviddoria 334 Posting Virtuoso Featured Poster

Here is an example of the nested vectors that Nick Evan mentioned:
http://programmingexamples.net/index.php?title=CPP/2DVector

Also, what is going on here?

array[n][n]>>board[i][j];
daviddoria 334 Posting Virtuoso Featured Poster

You may want to ask on the OpenSSL mailing list: http://www.openssl.org/support/community.html

daviddoria 334 Posting Virtuoso Featured Poster

I have no idea what is going on there, but if it is in an infinite loop then flag is never getting set to 1.

I STRONGLY suggest breaking a lot of this code out into functions. I.e.

void GenerateRandomNumbers(int rands[][])
{
	    rands[i][0] = 600.0 * rand()/RAND_MAX;    // for the room number.
	    rands[i][1] = 100.0 * rand()/RAND_MAX;    // for the wumpus.
	    rands[i][2] = 100.0 * rand()/RAND_MAX;    // for the pit.
	    rands[i][3] = 100.0 * rand()/RAND_MAX;    // for the bat.
}

It looks like you have have at least 5 or 10 of this size functions. Then your main function should be easy for us (and you, more importantly) to follow:

main()
{
  getInput();
  while(some condition)
    {
    doSomething();
    doSomethingElse();
    if(a condition)
      {
      doAnotherThing();
      }
    }
}

David

daviddoria 334 Posting Virtuoso Featured Poster

I would do two things to start debugging.

First, get rid of the user input and hardcode some values. Once it works with the hardcoded values, you can put the user input back in.

Second, write out the steps of the algorithm for the same values you have hard coded. Use cout statments (or better, a debugger) at every place you can to track the values. You should be able to see when they vary from your hand-written track through the algorithm, which should hopefully point you to the bug.

Give it a shot and let us know if you get stuck.

David

daviddoria 334 Posting Virtuoso Featured Poster

Cool&Awesome,

Thanks for the detailed reply!

With this second method, is it necessary to use an actual database? Is there no way to pull these things from a file? Something like:

<title><?php get contents of title tag from somefile.php ?></title>

?

If not, I'll just stick with the first method you suggested.

David

daviddoria 334 Posting Virtuoso Featured Poster

I have recently taken over as a volunteer webmaster for this website:
http://www.ewh.ieee.org/r1/schenectady/newsletter.html

Under the "Schenectady Section News" heading, you can see that there are a bunch of simple pages that all look the same. The current process to add a new one is to copy and paste an old one and modify the text. This seems to violate the principle of "never duplicate code". I was hoping for something more like a "shell" which contains the header etc, that I can point to something like an xml file:

Shell:

<html>
<body>
... all other header stuff...

Place <titletag> here (see below)

Place <contenttag> here (also see below)

</html>

One content example:

<titletag>
Title goes here
</titletag>

<contenttag>
Content goes here
</contenttag>

Can anyone suggest an easy way to do this? I'd be willing to learn a "complicated" way, but this needs to be transferable to the next volunteer, who may not be so willing :)

Thanks!

David

daviddoria 334 Posting Virtuoso Featured Poster

Your + operator should return the same type as the inputs:

Homework Homework::operator+ (const Homework rhs) const {
   
    Homework result;
    int a = this->time;
    int b = rhs.time;
    result.SetTime(a+b);
    return result;
}
brandonrunyon commented: Thanks for your assistance, good clearity, sorry to make you repeat yourself :-\ +4
daviddoria 334 Posting Virtuoso Featured Poster

The problem is that your + function returns an int. Your templated function was trying to return the same type as your inputs. So when you add a Homework to a Homework, it was trying to return a Homework, but your + function was returning an int, so it didn't make sense. Check out the working version below.

#include <iostream>
 
 class Homework
 {
   int time;
   
   public:
     void SetTime(int t){time = t;}
     int operator+ (const Homework rhs) const;
 };
 
template <class T>
int sum (T a, T b) {
  int result = a+b;
  return result;
}

//lhs.time + rhs.time = int result

int Homework::operator+ (const Homework rhs) const {
   
    int result;
    int a = this->time;
    int b = rhs.time;
    result  = (a+b);
    return result;
}

int main(int argc, char *argv[])
{
  Homework hw1;
  hw1.SetTime(1);
  
  Homework hw2;
  hw2.SetTime(2);
  
  {
  int total = hw1 + hw2; 
  std::cout << total << std::endl; 
  }
  
  int total = sum<Homework> (hw1, hw2);
  std::cout << total << std::endl; 

  return 0;
}

See what I mean?

David

daviddoria 334 Posting Virtuoso Featured Poster

I strongly suggest you add many comments to your code. At the very least, there should be one or two sentences at beginning of every function telling the reader what the function is supposed to do.

You can also give us a sense of what the entire program is supposed to do.

Also, whenever I see something like this:

int conInput1[ARRAY_SIZE] = {7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};

it makes me think something is being done less than efficiently :)

daviddoria 334 Posting Virtuoso Featured Poster

Your choice of those two functions seems fine, you just have to implement them! Here is a good place to start:
http://programmingexamples.net/index.php?title=CPP

Particularly: http://programmingexamples.net/index.php?title=CPP/IO/FileInput and http://programmingexamples.net/index.php?title=CPP/IO/ReadingLines .

Also, please use code tags when posting code.

David

daviddoria 334 Posting Virtuoso Featured Poster

The templated sum() function tries to add a to b. Your + overload adds a.time to b.time. It should work if you call:

int total = sum<int> (hw.time, hw2.time);

David

daviddoria 334 Posting Virtuoso Featured Poster

If it is a complicated structure, there is probably not a better way than to require the user to use multiple of your classes (the classes inside (composing) the big class). Sounds fine to me.

daviddoria 334 Posting Virtuoso Featured Poster

You got it.

daviddoria 334 Posting Virtuoso Featured Poster

Sure, just keep the files on the usb drive! VC won't care whether they are on the C drive or some other (F, perhaps) drive.

daviddoria 334 Posting Virtuoso Featured Poster

Fbody,

Ah, yes, it worked when I passed by reference, thanks.

Another alternative I found:

#include <itkImage.h>
#include <itkImageFileWriter.h>
#include <itkRescaleIntensityImageFilter.h>

template <class T>
void WriteImage(typename T::Pointer image, std::string filename);

int main(int, char *[])
{
  itk::Image<unsigned char, 2>::Pointer image = itk::Image<unsigned char, 2>::New();
  WriteImage<itk::Image<unsigned char, 2> >(image, "test.jpg");

  return EXIT_SUCCESS;
}

template <class T>
void WriteImage(typename T::Pointer image, std::string filename)
{
  typedef itk::RescaleIntensityImageFilter<T, T> RescaleFilterType;
  typename RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New(); //note the use of typename here
}

itk::RescaleIntensityImageFilter<T,T>::Pointer is a "dependent name" -- i.e. it depends on the template parameter T, so you have to use typename for the filter as well.

Thanks for all of the discussion/help!

David

daviddoria 334 Posting Virtuoso Featured Poster

They are smart pointers.

But the classes all have:

Image(const Self &);          //purposely not implemented

So I get:

error: ‘itk::Image<TPixel, VImageDimension>::Image(const itk::Image<TPixel, VImageDimension>&) [with TPixel = unsigned char, unsigned int VImageDimension = 2u]’ is private

when I do this:

#include <itkImage.h>
#include <itkImageFileWriter.h>
#include <itkRescaleIntensityImageFilter.h>

template <class T>
void WriteImage(T image, std::string filename);

int main(int, char *[])
{
  itk::Image<unsigned char, 2>::Pointer image = itk::Image<unsigned char, 2>::New();
  WriteImage<itk::Image<unsigned char, 2> >(*(image), "test.jpg");

  return EXIT_SUCCESS;
}

template <class T>
void WriteImage(T image, std::string filename)
{

}
daviddoria 334 Posting Virtuoso Featured Poster

Right, so that non-deducibility can be resolved by simply placing T in the angle brackets before the parameter list in the function call:

function<T>(params)

The problem here seems to be re-using the template parameter as the parameter for another template for some reason?

daviddoria 334 Posting Virtuoso Featured Poster

Yes, I'm sure.

This works

itk::RescaleIntensityImageFilter<itk::Image<unsigned char, 2> ,   
    itk::Image<unsigned char, 2> >::Pointer RescaleFilterType =
    itk::RescaleIntensityImageFilter<itk::Image<unsigned char, 2> ,   
    itk::Image<unsigned char, 2> >::New();

and this doesn't

itk::RescaleIntensityImageFilter<itk::Image<unsigned char, 2>::Pointer , 
  itk::Image<unsigned char, 2>::Pointer >::Pointer RescaleFilterType =
  itk::RescaleIntensityImageFilter<itk::Image<unsigned char, 2>::Pointer , 
  itk::Image<unsigned char, 2>::Pointer >::New();

(now you see why I use typedefs? hehe)

daviddoria 334 Posting Virtuoso Featured Poster

That would work fine if all I needed to do was use 'image' inside the function. The problem is that I have instantiate this other class, RescaleIntensityImageFilter, using the type WITHOUT the ::Pointer

typedef itk::RescaleIntensityImageFilter< itk::Image<unsigned char, 2> ,  itk::Image<unsigned char, 2> > RescaleFilterType;

Is there a way to do that with the method you've suggested?

daviddoria 334 Posting Virtuoso Featured Poster

The problem is that the image is type

itk::Image<unsigned char, 2>::Pointer

but the template paramters for the RescaleIntensityImageFilter need to be just

itk::Image<unsigned char, 2>

I got some help here the other day with a similar problem - the solution was that the ::Pointer was a "non-deducible context" and that's why I had to add the template parameter in the call:

WriteImage< itk::Image<unsigned char, 2> > (image, "test.jpg");

rather than just:

WriteImage(image, "test.jpg");

Any other ideas? These things are very frustrating sometimes...

daviddoria 334 Posting Virtuoso Featured Poster

Yep, it takes two template parameters:

http://www.itk.org/Doxygen/html/classitk_1_1RescaleIntensityImageFilter.html

This compiles no problem:

template <class T>
void WriteImage(typename T::Pointer image, std::string filename)
{
  //typedef itk::RescaleIntensityImageFilter<typename T, typename T> RescaleFilterType; //template argument 1 is invalid
  //typedef itk::RescaleIntensityImageFilter<T, T> RescaleFilterType; // expected ';' before rescaleFilter

  typedef itk::RescaleIntensityImageFilter< itk::Image<unsigned char, 2> ,  itk::Image<unsigned char, 2> > RescaleFilterType;
  RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();

}
daviddoria 334 Posting Virtuoso Featured Poster

When trying to pass a template parameter to another template, I am getting an 'invalid template parameter' error. I even tried to force it with 'typename' with no success. Can anyone see what's wrong with this?

#include <itkImage.h>
#include <itkImageFileWriter.h>
#include <itkRescaleIntensityImageFilter.h>

template <class T>
void WriteImage(typename T::Pointer image, std::string filename);

int main(int, char *[])
{
  itk::Image<unsigned char, 2>::Pointer image = itk::Image<unsigned char, 2>::New();
  WriteImage<itk::Image<unsigned char, 2> > (image, "test.jpg");

  return EXIT_SUCCESS;
}

template <class T>
void WriteImage(typename T::Pointer image, std::string filename)
{
  typedef itk::RescaleIntensityImageFilter<typename T, typename T> RescaleFilterType; //template argument 1 is invalid
  //typedef itk::RescaleIntensityImageFilter<T, T> RescaleFilterType; // expected ';' before rescaleFilter

  RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();

}

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

It seems like you are explaining two things. The first is that the program opens, reads a file, updates the file, then closes. The second is that you are doing this read/write in a while loop?

fstream is definitely the way to go for the file IO. Then you should use a stringstream to convert the string you read to the type (int) you want.

These may help you with some syntax:
http://programmingexamples.net/index.php?title=CPP/IO/ReadingLines
http://programmingexamples.net/index.php?title=CPP/IO/FileOutput
http://programmingexamples.net/index.php?title=CPP/IO/FileInput

David

daviddoria 334 Posting Virtuoso Featured Poster

I believe the idea of inline functions is that it will make small functions run faster. It seems like an "old school" concept - can someone testify that it still makes a significant difference on modern compilers? Maybe provide a short example program and some timings? This seems like a good candidate for something to go in the 'code snippets' or 'tutorials' section of the forum.

daviddoria 334 Posting Virtuoso Featured Poster

1) First, you should include cstdlib:

#include <cstdlib>

Not doing so causes compiler errors on many systems.

2) Another, more serious error is:

cout<< count << " is located at col " << countNum(ar,10,col)<<" row " <<countNum(ar,10,row)<<endl;

'col' and 'row' are not defined! You need to return them by reference from your countNum function I would assume (see #3).

3) COMMENT COMMENT COMMENT!!! There is no reason to shorten variable names ("ar" I'm assuming is array?) Also, your functions should have comments describing what they do. What does countNum do? What is the input argument 'size'? What about 'find'? Writing these things will help you keep track of what is going on.

David

daviddoria 334 Posting Virtuoso Featured Poster

I don't know why this isn't part of the main SDL library, but SDL_draw seems to have what you want:
http://sdl-draw.sourceforge.net/

daviddoria 334 Posting Virtuoso Featured Poster

Mike,

Thanks, that does the job. I don't mind telling it the template argument, I'll always know what it is. I didn't realize you could just tell it in <> before the argument list.

David

daviddoria 334 Posting Virtuoso Featured Poster

What are CArray and CString?

To set a member of a struct, you first have to instantiate it:

student myStudent;

Then you just use a '.' :

myStudent.Struct_Name = "David";
daviddoria 334 Posting Virtuoso Featured Poster

What are you trying to do?

daviddoria 334 Posting Virtuoso Featured Poster

If I use a template function with T where T = itk::Image<unsigned char>::Pointer, everything is fine:

#include <itkImage.h>

class Test
{
  public:
  template <class T>
  void Add(T patch);
};

template <class T>
void Test::Add(T patch)
{

}

int main(int, char*[])
{
  Test a;
  itk::Image<unsigned char>::Pointer image;
  a.Add(image);
  return EXIT_SUCCESS;
}

However, if I use a template function with a parameter T::Pointer where T = itk::Image<unsigned char>, I get errors:

#include <itkImage.h>

class Test
{
  public:
  template <class T>
  void Add(typename T::Pointer patch);
};

template <class T>
void Test::Add(typename T::Pointer patch)
{

}

int main(int, char*[])
{
  Test a;
  itk::Image<unsigned char>::Pointer image;
  a.Add(image);
  return EXIT_SUCCESS;
}
error: no matching function for call to ‘Test::Add(itk::SmartPointer<itk::Image<unsigned char, 2u> >&)’

Can someone explain why these are different, and how to fix the second one?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

You can also put

double mean(int arraym[299][299], int minl, int minw, int rlm, int rwm);

(note the addition of the semicolon at the end)

before main, then exactly the same thing you had before after main:

double mean(int arraym[299][299], int minl, int minw, int rlm, int rwm)
{
  double sum(0);
  int n(0);
  for (int x=minl; x<=rlm; x++)
  {
    for (int y=minw; y<=rwm; y++)
      {
        sum += arraym[x][y];
        n++;
      }
  }
  return sum/n;
}
daviddoria 334 Posting Virtuoso Featured Poster

Search sounds like a good way to go. I don't know what you mean by it uses "an array to search", but this shouldn't use any extra memory, it looks you just specify a range of indices which is your search patter, and a range of indices in which to search.

daviddoria 334 Posting Virtuoso Featured Poster

You just need to change

variance(arraym[299][299], minl, minw, rlm, rwm);

to

variance(arraym, minl, minw, rlm, rwm);

This compiles for me:

#include <cstdlib>
#include <iostream>
#include <string>

//mean function
double mean(int arraym[299][299], int minl, int minw, int rlm, int rwm)
{
  double sum(0);
  int n(0);
  for (int x=minl; x<=rlm; x++)
  {
    for (int y=minw; y<=rwm; y++)
      {
        sum += arraym[x][y];
        n++;
      }
  }
  return sum/n;
}

//variance function
double variance(int arraym[299][299], int minl, int minw, int rlm, int rwm)
{
  double sum(0), me;
  int n(0);
  me = mean(arraym, minl, minw, rlm, rwm);
  for (int x=minl; x<=rlm; x++)
  {
    for (int y=minw; y<=rwm; y++)
      {
        sum += (arraym[x][y] - me)*(arraym[x][y] - me);
        n++;
      }
  }
  return sum/(n-1);
}

int main(int argc, char *argv[])
{
  int arraym[299][299];
  int minl, minw, rlm, rwm;
  double y = variance(arraym, minl, minw, rlm, rwm);
  return 0;
}