Posting Freak
Posts: 9,907
Threads: 379
Joined: 2010-02
Imagine Wrote:Professors don't want me to using switch, and haven't learned std:: ispunct.
The point of the cin function is to read the document word by word, so it isn't infinite as long as a text document is inputted
Then you'd use std::ifstream not std::cin.
Posting Freak
Posts: 1,369
Threads: 48
Joined: 2010-06
Gender: Male
Country Flag: usa
IGN: HeroSogeking
Guild: KoopaForce
Guild Alliance: KoopaEmpire
Stuck on another problem:
"Consider this data sequence: "3 11 5 5 5 2 4 6 6 7 3 -8". Any value that is the same as the immediately preceding value is considered a CONSECUTIVE DUPLICATE. In this example, there are three such consecutive duplicates: the 2nd and 3rd 5s and the second 6. Note that the last 3 is not a consecutive duplicate because it was preceded by a 7.
Write some code that uses a loop to read such a sequence of non-negative integers, terminated by a negative number. When the code exits the loop it should print the number of consecutive duplicates encountered. In the above case, that value would be 3."
My original idea was to have:
Code: dupecount = 0;
while (cin << n) {
int prevNum = n;
while (cin << n) {
if (prevNum == n) {
++dupecount;
}
}
}
cout << dupecount;
But it doesn't work, since it seems to analyzes groups of 2 rather than consecutive integers. So, how do I analyze consecutive integers/strings/characters using only the while loop?
Posting Freak
Posts: 11,999
Threads: 634
Joined: 2009-07
Rather, it seems to compare all the numbers to the very first.
Your inner loop is only exited when the end of input is reached, so prevNum never gets updated.
You don't need two loops, just one. The inner "while" should just be an "if". And "prevNum = n;" should happen at the bottom of the loop, not the top.
When you have a program like this that doesn't do what you want, you should either use a debugger to go step by step and see where things don't happen like you intended them to, or run it "by hand" - pretend you're the computer and "execute" the program according to the input.
Also, it should be "cin >> n" - I assume that's a typo, because I don't think "cin << n" would compile.
Posting Freak
Posts: 1,369
Threads: 48
Joined: 2010-06
Gender: Male
Country Flag: usa
IGN: HeroSogeking
Guild: KoopaForce
Guild Alliance: KoopaEmpire
SaptaZapta Wrote:-snip-
Rearranged it + fixed typos.
Code: int n;
int dupecount = 0;
int count = 0; //prevNum is not defined in the 1st number, so I put this in.
int prevNum;
while (cin >> n) {
if (cin >> n && count != 0) {
if (prevNum == n) {
++dupecount;
}
}
int prevNum = n;
++count;
}
cout << dupecount;
The inputs I got wrong and the inputs that I got right switched.
Posting Freak
Posts: 9,907
Threads: 379
Joined: 2010-02
This works fine. Unless I misunderstood your problem.
You can replace vector with an array if you haven't learned it yet.
Code: #include <iostream>
#include <vector>
/*
"Consider this data sequence: "3 11 5 5 5 2 4 6 6 7 3 -8".
Any value that is the same as the immediately preceding value is considered a CONSECUTIVE DUPLICATE.
In this example, there are three such consecutive duplicates: the 2nd and 3rd 5s and the second 6.
Note that the last 3 is not a consecutive duplicate because it was preceded by a 7.
*/
int main() {
std::vector<int> holder;
size_t total = 0;
int a = 0;
while(std::cin >> a) {
holder.push_back(a);
if(a < 0)
break;
}
for(size_t i = 1; i < holder.size(); ++i) {
if(holder[i] == holder[i-1])
++total;
}
std::cout << "Total consecutives: " << total;
std::cin.get();
std::cin.get();
}
Posting Freak
Posts: 1,369
Threads: 48
Joined: 2010-06
Gender: Male
Country Flag: usa
IGN: HeroSogeking
Guild: KoopaForce
Guild Alliance: KoopaEmpire
Locked Wrote:-snip- Not supposed to be learning vectors or arrays yet, so I can't use them. I'm supposed to create a solution using only while loops.
Also, the header:
#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip>
using namespace std;
int main () {
Has been declared for the problem, and cannot be changed in any way.
Posting Freak
Posts: 11,999
Threads: 634
Joined: 2009-07
Does the rearranged version work right?
Posting Freak
Posts: 9,907
Threads: 379
Joined: 2010-02
lol'd.
How slow are you going to not learn about arrays yet?
Code: #include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip>
using namespace std;
/*
"Consider this data sequence: "3 11 5 5 5 2 4 6 6 7 3 -8".
Any value that is the same as the immediately preceding value is considered a CONSECUTIVE DUPLICATE.
In this example, there are three such consecutive duplicates: the 2nd and 3rd 5s and the second 6.
Note that the last 3 is not a consecutive duplicate because it was preceded by a 7.
*/
int main() {
unsigned int total = 0;
int a = 0;
int prevNumber = -1;
while(cin >> a) {
if(a == prevNumber)
++total;
if(a < 0)
break;
prevNumber = a;
}
cout << "Total Consecutive: " << total;
cin.get();
cin.get();
}
The way this program works is it assigns a value to prevNumber that is negative, this way it doesn't allow the user to input a digit to break its check. The check is done by checking if the input is the same as the previously assigned prevNumber. If it passes, it increments the total by one. If the number is less than 0, it breaks the loop and prints out the value. After both checks it assigns the prevNumber variable to a. cin.get() is there to pause the program for me.
Posting Freak
Posts: 1,369
Threads: 48
Joined: 2010-06
Gender: Male
Country Flag: usa
IGN: HeroSogeking
Guild: KoopaForce
Guild Alliance: KoopaEmpire
Pretty slowly.
What does size_t total = 0; do?
EDIT: And what would I do if I were dealing with strings rather than integers?
Posting Freak
Posts: 9,907
Threads: 379
Joined: 2010-02
Imagine Wrote:Pretty slowly.
What does size_t total = 0; do?
EDIT: And what would I do if I were dealing with strings rather than integers?
Ignore size_t, it's the biggest unsigned size possible on your system.
If you were dealing with strings chances are you would know arrays wouldn't you? const char*, std:  tring, and char[] would be the way to initiate a string in C++ (though std:  tring is preferred). Loop should almost be the same, instead you'd probably use getline(std::cin,string) for std:  tring and awful cin.getline for C-style strings.
Posting Freak
Posts: 1,369
Threads: 48
Joined: 2010-06
Gender: Male
Country Flag: usa
IGN: HeroSogeking
Guild: KoopaForce
Guild Alliance: KoopaEmpire
Locked Wrote:Ignore size_t, it's the biggest unsigned size possible on your system.
If you were dealing with strings chances are you would know arrays wouldn't you? const char*, std: tring, and char[] would be the way to initiate a string in C++ (though std: tring is preferred). Loop should almost be the same, instead you'd probably use getline(std::cin,string) for std: tring and awful cin.getline for C-style strings.
Nope, I don't know arrays.
I don't even know what anything that starts with std:: means.
Posting Freak
Posts: 9,907
Threads: 379
Joined: 2010-02
Imagine Wrote:Nope, I don't know arrays.
I don't even know what anything that starts with std:: means.
This is why using namespace std is cancerous.
(Not your fault don't worry)
Posting Freak
Posts: 1,369
Threads: 48
Joined: 2010-06
Gender: Male
Country Flag: usa
IGN: HeroSogeking
Guild: KoopaForce
Guild Alliance: KoopaEmpire
How would I deal with:
" Consider this data sequence: "fish bird reptile reptile bird bird bird mammal fish". Let's define a SINGLETON to be a data element that is not repeated immediately before or after itself in the sequence. So, here there are four SINGLETONs (the first appearance of "fish", the first appearance of "bird", "mammal", and the second appearance of "fish").
Write some code that uses a loop to read a sequence of words, terminated by the "xxxxx". The code assigns to the variable n the number of SINGLETONs that were read. (For example in the above data sequence it would assign 4 to n). Assume that n has already been declared but not initialized. Assume that there will be at least one word before the terminating "xxxxx". "
Posting Freak
Posts: 9,907
Threads: 379
Joined: 2010-02
I'm not sure how you'd use strings without an array. A singleton as a design process is pretty bad but I believe in here it means an array or tuple with only one element. Are you sure you've never learned arrays? God forbid, did you learn pointers before arrays?
Posting Freak
Posts: 1,369
Threads: 48
Joined: 2010-06
Gender: Male
Country Flag: usa
IGN: HeroSogeking
Guild: KoopaForce
Guild Alliance: KoopaEmpire
Locked Wrote:I'm not sure how you'd use strings without an array. A singleton as a design process is pretty bad but I believe in here it means an array or tuple with only one element. Are you sure you've never learned arrays? God forbid, did you learn pointers before arrays?
Current Chp: Chp 4 (loops)
Arrays: Chp 5
Pointers: Chp 8
Nope.
Posting Freak
Posts: 9,907
Threads: 379
Joined: 2010-02
A string is, like I said:
const char*
char*
char[]
and
std:  tring
You can't use either of those I imagine.
Posting Freak
Posts: 1,369
Threads: 48
Joined: 2010-06
Gender: Male
Country Flag: usa
IGN: HeroSogeking
Guild: KoopaForce
Guild Alliance: KoopaEmpire
Locked Wrote:A string is, like I said:
const char*
char*
char[]
and
std: tring
You can't use either of those I imagine.
I have not seen any of those so far.
Posting Freak
Posts: 9,907
Threads: 379
Joined: 2010-02
Imagine Wrote:I have not seen any of those so far.
How do you use strings then?
Posting Freak
Posts: 1,369
Threads: 48
Joined: 2010-06
Gender: Male
Country Flag: usa
IGN: HeroSogeking
Guild: KoopaForce
Guild Alliance: KoopaEmpire
Locked Wrote:How do you use strings then?
I use it for text inputs.
Posting Freak
Posts: 9,907
Threads: 379
Joined: 2010-02
Imagine Wrote:I use it for text inputs.
How would you store the text? What data type do you use that it doesn't involve arrays or pointers?
|