Open Side Menu Go to the Top
Register
First Programming Class: C++ First Programming Class: C++

05-23-2013 , 10:35 PM
In the while loop declare

int lettersCount[26];

Then pass in pointer to array.

void counter(char ch, int* lettersCount)
{

// *(lettersCount + index) etc..
First Programming Class: C++ Quote
05-23-2013 , 11:54 PM
Just saw your response. Thank you.

If I want to go character by character from a txt file, preserving white spaces, is there something I can do with getline() to make that work?

HTML Code:
while (!fromFile.eof()){
	fromFile >> ch; // replacement for this line
}
First Programming Class: C++ Quote
05-24-2013 , 12:56 AM
you can use getc
or you can read the whole line into a string and parse it one at a time
First Programming Class: C++ Quote
05-24-2013 , 02:11 AM
Quote:
Originally Posted by jmark
you can use getc
or you can read the whole line into a string and parse it one at a time
I'm not familiar with how File objects work. I read a bit on getc, but it's a bit beyond me.

I also must not know how reading the whole line into a string really works. I can do that, but how does that help me sort individual characters? I still have to move through the string char by char right? If there is a way to NOT skip the whitespace while doing that operation wouldn't it be the same operation as if i read from my fstream char by char?

HTML Code:
while (!fromFile.eof()){
		 fromFile >> ch;  // I know this will never work
		if(islower(ch)){
			ch -=shift;
			if(ch < 'a'){
				ch+=26;
			}
		}
		if(isupper(ch)){
			ch -=shift;
			if(ch < 'A'){
				ch +=26;
			}
		}

	cout << static_cast<char>(ch);
	
	}
First Programming Class: C++ Quote
05-24-2013 , 10:04 AM
Alright, after much frustration I have a code that does what I want. The problem is that I'm not comfortable with functions and as a result I've got one big ugly main function.

The biggest obstacle is that I'm still a bit confused about what parameters I would pass through to my functions as well as how to have my function access data from my ifstream variable.

As I see it, there is, at minimum, two functions that I could make using the while loops in my main, but maybe three. My main function does the following:

1) Ask user for file name and open file. If file !open then we output an appropriate response

2) (using a while(!eof) loop) A) Read character by character to track occurrences of a letter. B) Create a "shift" value that will shift each letter by an amount that makes our highest occurring letter == 'e'.

3) (using a while(!eof) loop) Read character by character & manipulate each letter using the shift value while preserving whitespace/punctuation and print new letters to the screen.

I'll post the beast below:

HTML Code:
// This is the Caesar Cipher program
// This program will read in data and decypher it
// Chase Banta
// 24 may 2013
// Sources:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
	
	// Declaring variables for use throughout program
	ifstream fromFile;
	string fileName;
	char ch;
	int letterCount[26] = {0};
	int index; 
	int mostCommon = 0;
	int shift = 0;
	int tracker = 0;
	string wholeDoc;


	// Prompting user to enter file name and opening file
	cout << "Please enter the file name you wish to decypher : "; 
	cin >> fileName;
	cout << endl << endl;
	fromFile.open(fileName.c_str());
	
	// If a file is not found in the location suggested by the user 
	// an appropriate error message outputs and the program quits.
	if (!fromFile.is_open()) {
		cout << "An error has occured.  File name not found." << endl;
		getchar();
		getchar();
		return 0;
	}
	
	while (!fromFile.eof()){
		fromFile >> ch;
		ch = toupper(ch);
		index = ch -'A';
		if(index < 26 && index >= 0) {
			letterCount[index]++;
		}
	}
	
	for(index = 0; index < 26; index++)	{
		if(letterCount[index] > tracker) {
			tracker = letterCount[index]; 
			mostCommon  = index;
		}
	}
	shift = (('A'+ mostCommon)-'E') ;

	fromFile.clear();
	fromFile.seekg(0);
	while (!fromFile.eof()){
		 fromFile >> ch;
		if(islower(ch)){
			ch -=shift;
			if(ch < 'a'){
				ch+=26;
			}
		}
		if(isupper(ch)){
			ch -=shift;
			if(ch < 'A'){
				ch +=26;
			}
		}

	cout << static_cast<char>(ch);
	
	}
	getchar();
	getchar();
	return 0;
}
The only way that this program doesn't do what I would like is that when it prints out, it doesn't "text wrap". Is there something I can use from the iomanip library for that?
First Programming Class: C++ Quote
05-24-2013 , 01:32 PM
I don't see why you're casting to a char in your cout, when ch is already a char. Do you have a known length you want to text wrap to? Like every 40 characters you want a newline?
First Programming Class: C++ Quote
05-24-2013 , 02:58 PM
Quote:
Originally Posted by jmark
I don't see why you're casting to a char in your cout, when ch is already a char.
Fixed. For some reason I thought that it would only print out integers if I didn't do that.


Quote:
Do you have a known length you want to text wrap to? Like every 40 characters you want a newline?
I'm estimating about 72 characters I need a newline.
First Programming Class: C++ Quote
05-24-2013 , 05:10 PM
If you only want a newline every 72 characters, it's pretty easy to add that to your last while loop. But if you want to only break on spaces then you'll have to look ahead to the next word and count its length then decide whether to newline before starting it.
First Programming Class: C++ Quote
05-27-2013 , 10:40 AM
Quote:
Originally Posted by Chips Ahoy
This is a floating point comparison. The basic problem:

http://www.cygnus-software.com/paper...ringfloats.htm

Code:
float f = 0.1f;
float sum;
sum = 0;
 
for (int i = 0; i < 10; ++i)
    sum += f;
float product = f * 10;
printf("sum = %1.15f, mul = %1.15f, mul2 = %1.15f\n",
        sum, product, f * 10);


As a general rule, floating point results are inexact. Prudent, experienced C programmers do not compare the result of a floating point calculation for equality. Instead they check if the result is "close enough" -- within some epsilon.

Consider a sqrt implementation that returns 2.9999999999996443 for sqrt(9). In that case -- without the + 0.01 -- IsPrime(9) would return true!

Now I recall from having worked with lua that integers have exact representations as doubles:


http://www.lua.org/pil/2.3.html

Which is helpful. It means there are no cases that are guaranteed to fail below 10^14. It also means there are cases that are guaranteed to fail above 10^14. Single precision will fail lower.

There is the more serious matter of the implementation of pow(). That is a black box and could have accumulated error in it afaik.



oic.

I think "always use an epsilon instead of equality checks with floating point" is a pretty good rule. The result is code that always works. The alternative is either a hard to find corner case or spending precious developer time deciding an equality check will work.
this is all very long-winded cover for:

Quote:
I'm not sure about when a floating point is going to be exactly an integer.
...being insanely stupid. also you can round floats up or down and i doubt very much that you are a prudent programmer or a programmer at all. please stop posting for real. i forgot this was the same thread that had people arguing about text editors being debuggers as well.

http://en.wikipedia.org/wiki/Dunning...3Kruger_effect ITT.
First Programming Class: C++ Quote
05-27-2013 , 11:14 AM
you sure are an *******
First Programming Class: C++ Quote
05-27-2013 , 11:21 AM
Quote:
Originally Posted by tpir
this is all very long-winded cover for:


Quote:
I'm not sure about when a floating point is going to be exactly an integer.
...being insanely stupid. also you can round floats up or down and i doubt very much that you are a prudent programmer or a programmer at all. please stop posting for real. i forgot this was the same thread that had people arguing about text editors being debuggers as well.

http://en.wikipedia.org/wiki/Dunning...3Kruger_effect ITT.
Code:
#include <stdio.h>
#include <limits.h>

int main(int argc, char* argv[])
{
	for(int i = 0; i < INT_MAX; i++)
	{
		float f = (float)i;
		int check = (int)f;
		if(check < i)
		{
			printf("%d has no exact representation as a float, and the float is smaller.\n", i);
			getchar();
			break;
		}
	}
	
	return 0;
}
16777217 has no exact representation as a float, and the float is smaller.
First Programming Class: C++ Quote
05-27-2013 , 12:16 PM
Using my psychic powers I have deduced what tpir is thinking.

Code:
checker <= pow(n , .5)
checker will be promoted to a double for this comparison so the exact representation of a float as an int is not relevant here.

I still fail to understand how he knows there will be no epsilon of error in the pow function.
First Programming Class: C++ Quote
05-27-2013 , 01:12 PM
Quote:
Originally Posted by Chips Ahoy
I still fail to understand how he knows there will be no epsilon of error in the pow function.
Code:
#include <stdio.h>
#include <limits.h>
#include <math.h>
int main(int argc, char* argv[])
{
	for(int i = 1; i < INT_MAX; i++)
	{
		float sq = i*(float)i;
		float root = powf(sq, 0.5);
		if(root != i)
		{
			printf("The sqrt of %d squared is not exact.\n", i);
			getchar();
			break;
		}
	}	
	return 0;
}
The sqrt of 16777217 squared is not exact.

---

It appears there is error in the pow function.
First Programming Class: C++ Quote
05-27-2013 , 07:47 PM
Quote:
Originally Posted by Chips Ahoy
Code:
#include <stdio.h>
#include <limits.h>
#include <math.h>
int main(int argc, char* argv[])
{
	for(int i = 1; i < INT_MAX; i++)
	{
		float sq = i*(float)i;
		float root = powf(sq, 0.5);
		if(root != i)
		{
			printf("The sqrt of %d squared is not exact.\n", i);
			getchar();
			break;
		}
	}	
	return 0;
}
The sqrt of 16777217 squared is not exact.

---

It appears there is error in the pow function.
1. 16777217 can be represented as a float exactly

2. using the float type does not yield the exact sqrt. Use doubles along with pow and it does. In a nutshell I suspect the reason for this example is that that there are more bits in the exponent of a double than there are in a float.

3. Personally I rarely if ever see advantages to using floats instead of doubles but maybe there are. Conversely there are disadvantages to using floats instead of doubles.
First Programming Class: C++ Quote
05-29-2013 , 07:52 PM
Quote:
Originally Posted by adios
3. Personally I rarely if ever see advantages to using floats instead of doubles but maybe there are. Conversely there are disadvantages to using floats instead of doubles.
I did some reading.

There is an IEEE standard for floating point. It guarantees rounding will come out right for lots of operations. This can be done by keeping extra bits around when doing intermediate calculations. sqrt is essentially a single calculation so it comes out right with 64 bit doubles. I think the only errors will be the lack of bits in plain floats.

In the x86 world the native format for floating point is 80 bits (long double). This MSDN page says windows works with just 64 bit doubles natively. In cpp and c# double is the default float format, similar to how int is the default integer format.

There is a MS function to switch the precision at the processor if you really don't want 64 bits.

The place where 32bit floating point makes sense is on micros. I've worked on an 8051, which is slow and has no floating point support. A floating point library (all software) is going to be slow enough at 32 bits, 64 bits makes no sense there.

It occurs to me that casting to floats is also a way to check for "close enough" with arbitrary numbers. I've always used the constraints of a calculation to pick a reasonable bounds, but just casting away some of the least significant bits should work.
First Programming Class: C++ Quote
05-29-2013 , 07:56 PM
Quote:
Originally Posted by adios
1. 16777217 can be represented as a float exactly

2. using the float type does not yield the exact sqrt. Use doubles along with pow and it does. In a nutshell I suspect the reason for this example is that that there are more bits in the exponent of a double than there are in a float.
Ding!
First Programming Class: C++ Quote
05-29-2013 , 10:37 PM
The 80bit floating point stuff on x86 chips has been something of an anachronism for a long time now. Those instructions are part of the old stack-based "x87 FPU"... all but deprecated by arrival of the SSE system. I think you would be hard pressed to get a modern compiler to generate machine code that contained x87 fpu instructions.

Its also been that long since there was considered any real performance advantage to using integer math over floating point math. There is virtually no speed difference between fp and integer instructions on modern CPUs.

Likewise, there is basically no speed difference between 32 and 64 bit floating point instructions... *except* that because performance bottlenecks due to memory/cache bandwidth are so common, using 64 bit FP means twice the storage required making it that much more likely that you'll be impacted by those memory bandwidth issues.

And of course if you actually intend to write vectored SSE code... you can do 4x32bit ops at a time instead of 2x64bit.

So...

Integer vs FP? No big deal.

32bit vs 64bit FP? Use 32 bit if you dont need more than 7 decimal digits of precision and you want more cache friendly code.
First Programming Class: C++ Quote
05-30-2013 , 06:45 AM
On something like an 8051 that doesn't have floating point instructions my experience has been that "real number" type calculations are done using fixed point arithmetic using multiply and divide instructions. Choosing the bit position of the binary point usually involves some analysis.

On the double vs. float thing I have my doubts that memory bottlenecks / cache hits are a major concern in x86 multicore architectures very often. It is possible but I would opine rarely. As an aside in real-time applications memory bottlenecks can become a real concern and thus having a lot of registers can come in very handy for meeting real-time deadlines.
First Programming Class: C++ Quote

      
m