Say i have an array char myArrray[5] = {'T','T','T','T','T'} And i want to check how many instances of 3 pairs of T i have.
I have a forloop below that checks for all 3 instances of T. There should be 3 instances, but for some reason its not even entering the if statement that checks it.
Maybe im just lost im really sleepy.
There are 3 isntances of TTT throughout the array. Thats what we have to get the number 3 in the counter but we arent getting it. (T{T[T)T}T]
full code here: http://ideone.com/AWyOkH
Any ideas?
for(int k = 0; k < lineInputs; k++)
{
int counter=0;
cout << (k+1) << " ";
for(int u=0; u<arrayElements; u++)
{
//cout << myArray[u];
if(myArray[u] == 'T' && myArray[u+1] == 'T' && myArray[u+2] == 'T')
{
counter++;
cout << counter << " ";
}
}
}
Does the issue lay with if(myArray[u] == 'T' && myArray[u+1] == 'T' && myArray[u+2] == 'T') ?
Seems to work fine.
char myArray[5] = {'T','T','T','T','T'};
int lineInputs=1;
for(int k = 0; k < lineInputs; k++)
{
int counter=0;
cout << (k+1) << " ";
int arrayElements=5;
for(int u=0; u<(arrayElements-2); u++)
{
//cout << myArray[u];
if(myArray[u] == 'T' && myArray[u+1] == 'T' && myArray[u+2] == 'T')
{
counter++;
cout << counter << " ";
}
}
}
Output I get is:
1 1 2 3
It works fine, although the code you posted should have:
for(int u=0; u<arrayElements-2; u++)
Instead of:
for(int u=0; u<arrayElements; u++)
The output will be 1 1 2 3 because you first print k+1, and then the counter each time you find three consecutive "T"'s. What you have programmed is correct!
Related
I need to check if all digits in the one element in array = 0 or 1.
I have functions for this:
bool checkIfBin(int okt) {
while (okt > 0)
{
int digit = okt % 10;
if (digit > 1)
return false;
okt /= 10;
}
return true;
}
and calling this function looks like:
for (int j=0; j < 4; j++) {
cout << "Enter" << j+1 << " octet in binary system: ";
if (checkIfBin(okt[j]) == true)
cin >> okt[j];
else
break;
}
the problem is that - it doesn't stop program from looping when the function returns false. I've debugged functions and it seems to work fine, but I'm just learning and I could have done it wrong. does not receive any errors.
I don't know where the problem is
The problem stems from the fact that You are checking the input before reading it.
for (int j = 0; j < 4; j++)
{
cout << "Enter" << j+1 << " octet in binary system: ";
cin >> okt[j];
if (!checkIfBin(okt[j]))
break;
}
should work beautifully.
Thanks to all of you for the answers,
especially #WhozCraig
for (int j=0; j < 4; j++) {
cout << "Enter" << j+1 << " octet in binary system: ";
cin >> okt[j];
if ((!checkIfBin(okt[j])) break;
}
works fine
I am programming a guessing game where the user will define the first array to scale from 1 to whatever number inputed. I have a function that will randomise the numbers, then the second array will be the user input guesses. I am trying to make a for loop that will cycle through both arrays and print "O" if array values match, and "X" if they do not. It looks like the first iteration of the loop works and prints "O" but then prints a lot of X's after that.
#include <iostream>
using namespace std;
void createArray() {
int n;
int counter = 0;
cout << "Enter total number: ";
cin >> n;
// for loop to generate array to scale from 1 -
user input.
int *arr1 = new int[n];
for (int i = 1; i < n + 1; i++){
arr1[i] = i;
// Counter is used to track the size of array
counter++;
}
// for loop used to print the values of array (just for development and my reference)
for (int i = 1; i < n + 1; i++){
cout <<"[" << arr1[i] << "]";
}
cout << endl;
cout << counter << endl << "Number Guessing" << endl;
cout << "Enter " << counter << " digits (1-" << counter << ")" << endl;
// initialize user input array to be the same size as first array
int arr2[counter];
for (int i =0; i < counter; i++){
cin >> arr2[i];
}
cout << endl;
// here I am trying to cycle through both arrays and print "O" if the values match in each array.
for (int i = 0; i < n; i++){
arr1[i] = i;
bool correct = true;
for (int j = 0; j < n; j++){
arr2[j] = j;
if (arr1[i] != arr2[j]){
correct = false;
cout << "X";
}
if (correct){
cout << "O";
}
}
}
cout << endl;
}
int main(){
createArray();
}
This is the output:
Enter total number: 6
[1][2][3][4][5][6]
Number Guessing Enter 6 digits (1-6)
1 2 3 4 5 6
OXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Your loops are not comparing the arrays side by side but rather every element in array 1 is being compared to every element in array 2.
Since both arrays are reset by arr[i]=i & arr2[j]=j then they both are {0,1,2,3,4,5}.
The first innerloop will net a 'O' if(0==0) where i=0 & j=0, the next 5 innerloops if(0==1..5) where i=0 & j=1..5, are an 'X'.
The problem is on the next outerloop. Correct is reset to true by the outerloops second pass of i=1, but immediately the innerloop tests if(1==0) where i=1 & j=0, which is false and thus sets Correct to false.
So on the second pass thru the innerloop if(1==1) is true where i=1 & j=1, BUT there is no statement in the inner loop to set **Correct back to true.
Also, where as the X is printed as the result of the equality test, the O is outputted only when Correct is still true, which it is not. In fact, for every outloop after the first one, there will be an innerloop equality test which sets Correct to false before any a subsequent true equality tests.
The results are one 'O' followed by a 30 'X'. One 'X' for every false; 6 outerloops, and foreach, 5 of the six innerloops will be false. Five of the 'O's will simply not print because the 'O' does not print as a result of the equality test.
You dont need two loops
for (int i = 0; i <= n; ++i) {
if (arr1[i] == arr2[i]) {
std::cout << "0";
}
else {
std::cout << "X";
}
}
You should put your expected output for us to know what you want
I am trying to write a program to count each number the program has encountered. by putting M as an input for the number of the array elements and Max is for the maximum amount of number like you shouldn't exceed this number when writing an input in the M[i]. for some reason the program works just fine when I enter a small input like
Data input:
10 3
1 2 3 2 3 1 1 1 1 3
Answer:
5 2 3
But when I put a big input like 364 for array elements and 15 for example for max. the output doesn't work as expected and I can't find a reason for that!
#include "stdafx.h"
#include <iostream>
#include<fstream>
#include<string>
#include <stdio.h>
#include<conio.h>
using namespace std;
int ArrayValue;
int Max;
int M[1000];
int checker[1000];
int element_cntr = 0;
int cntr = 0;
int n = 0;
void main()
{
cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
cin >> ArrayValue>> Max;
for (int i = 0; i < ArrayValue; i++)
{
cin >> M[i];
checker[i]= M[i] ;
element_cntr++;
if (M[i] > Max)
{
cout << "the element number " << element_cntr << " is bigger than " << Max << endl;
}
}
for (int i = 0; i < Max; i++)
{
cntr = 0;
for (int j = 0; j < ArrayValue; j++)
{
if (M[n] == checker[j])
{
cntr+=1;
}
}
if (cntr != 0)
{
cout << cntr << " ";
}
n++;
}
}
You have general algorithm problem and several code issues which make code hardly maintainable, non-readable and confusing. That's why you don't understand why it is not working.
Let's review it step by step.
The actual reason of incorrect output is that you only iterate through the first Max items of array when you need to iterate through the first Max integers. For example, let we have the input:
7 3
1 1 1 1 1 2 3
While the correct answer is: 5 1 1, your program will output 5 5 5, because in output loop it will iterate through the first three items and make output for them:
for (int i = 0; i < Max; i++)
for (int j = 0; j < ArrayValue; j++)
if (M[n] == checker[j]) // M[0] is 1, M[1] is 1 and M[2] is 1
It will output answers for first three items of initial array. In your example, it worked fine because the first three items were 1 2 3.
In order to make it work, you need to change your condition to
if (n == checker[j]) // oh, why do you need variable "n"? you have an "i" loop!
{
cntr += 1;
}
It will work, but both your code and algorithm are absolutely incorrect...
Not that proper solution
You have an unnecessary variable element_cntr - loop variable i will provide the same values. You are duplicating it's value.
Also, in your output loop you create a variable n while you have a loop variable i which does the same. You can safely remove variable n and replace if (M[n] == checker[j]) to if (M[i] == checker[j]).
Moreover, your checker array is a full copy if variable M. Why do you like to duplicate all the values? :)
Your code should look, at least, like this:
using namespace std;
int ArrayValue;
int Max;
int M[1000];
int cntr = 0;
int main()
{
cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
cin >> ArrayValue >> Max;
for (int i = 0; i < ArrayValue; i++)
{
cin >> M[i];
if (M[i] > Max)
{
cout << "the element number " << i << " is bigger than " << Max << endl;
}
}
for (int i = 0; i < Max; i++)
{
cntr = 0;
for (int j = 0; j < ArrayValue; j++)
{
if (i == M[j])
{
cntr ++;
}
}
if (cntr != 0)
{
cout << cntr << " ";
}
}
return 0;
}
Proper solution
Why do you need a nested loop at all? You take O(n*m) operations to count the occurences of items. It can be easily counted with O(n) operations.
Just count them while reading:
using namespace std;
int arraySize;
int maxValue;
int counts[1000];
int main()
{
cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
cin >> arraySize >> maxValue;
int lastReadValue;
for (int i = 0; i < arraySize; i++)
{
cin >> lastReadValue;
if (lastReadValue > maxValue)
cout << "Number " << i << " is bigger than maxValue! Skipping it..." << endl;
else
counts[lastReadValue]++; // read and increase the occurence count
}
for (int i = 0; i <= maxValue; i++)
{
if (counts[i] > 0)
cout << i << " occurences: " << counts[i] << endl; // output existent numbers
}
return 0;
}
here is my code, im getting error no match for 'operator<=' in 'i <= slovo'
its a program which converts word in each row from capitals to lowercase...
can u help with this?? thanks
#include <iostream>
using namespace std;
int main ()
{
const int max = 100;
string slovo;
int pocet_r;
cout << "Zadaj pocet uloh:" << endl;
cin >> pocet_r;
if(pocet_r >= 1 && pocet_r <=100)
{
// funkcia na zabezpecenie minimalneho poctu chars
for (int i = 0; i <pocet_r; i++)
{
cout << "Uloha " << i+1 << ":" << endl;
cin >> slovo;
if(slovo.size() > max)
{
cout << "slovo musi mat minimalne 1 a maximalne 100 znakov" << endl;
}
while( slovo.size() > max)
{
cin >> slovo;
}
}
for (int i=0; i <= slovo; i++)
{
while (slovo[i] >= 'A' && slovo[i] <= 'Z')
{
slovo[i] = tolower(slovo[i]);
}
}
}else{
cout << "Minimalne 1 a maximalne 100 uloh" << endl;
}
system("pause");
}
i <= slovo tries to compare an integer to a string. With our mighty human brains, we know that 42 is acually larger than "This string", but the compiler isn't as smart, so it just doesn't let you compare integers to strings.
Did you mean to compare i to the string's length (i.e. .length() or .size())?
for (int i=0; i <= slovo.size(); i++)
// |
// You probably want < here though, not <=
slovo is a string, so i <= slovo doesn't make sense.
Did you mean to say i <= slovo.length()?
I think you have to use i <= slove.size() instead of i <= slove().
Is this program OK, or can it be improved (but simply)? How do I make sure no repeat numbers are entered?
int n;
int array[9];
cout<<"Enter Number Between 9-0 Only"<<endl;
for(int i = 0; i<=10; i++){
cout<<"Enter Number" <<(i+1)<<endl;
cin >> n;
if((n >= 0) && (n <=9)){
array[i]=n;
}
else{
cout<<"Numbers from 0-9 only\n"<<endl;
break;
}
}
(edit) complete, compiling code
To check if the numbers are used with higher performance, try something like this (using the working code from Jack Radcliffe):
#include <iostream>
using namespace std;
int main()
{
int n = 0;
int array[9] = {0};
bool isUsed[10] = {0};
for(int i = 0; i < 9; i++)
{
cout << "Enter Number " << (i + 1) << endl;
cin >> n;
if((n >= 0) && (n <= 9))
{
if (isUsed[n] == false)
{
array[i] = n;
isUsed[n] = true;
}
else
{
cout << "Number has already been used." << endl;
i--;
}
}
else
{
cout << "Numbers from 0-9 only." << endl;
i--;
}
}
return 0;
}
Optimization isn't exactly necessary with this simple of code, but it's this seems to be an exercise of practice, so why not practice optimized code, too?
Most of it is fine, though there are two problems standing out.
First, you have an array of size 9, but you are taking in 11 numbers since you're starting the for loop at 0 and going through to 10.
Second, since you have it so if the entered number is not between 0 and 9, inclusive, the for loop breaks. This entails that fewer than 9 numbers will be put into the array if an invalid number is entered. Change the entire loop to read this and you should be good:
for(int i = 0; i < 9; i++) {
cout << "Enter Number " << (i + 1) << endl;
cin >> n;
if((n >= 0) && (n <= 9))
array[i] = n;
else {
cout << "Numbers from 0-9 only\n" << endl;
i--;
}
}
The whole fire part was right, but I removed the break in the else-statement and added in the i--. I added that in so when the user is prompted to re-enter the number, the entry number will be at the correct index.
I hope this was helpful.