I am trying to make a code for one of the problems on an online coding platform. I have already made the code, but cannot get the input in the desired format. I need to take an input which has three integers (say n1, n2 and m). The question says that the input can be numbers separated by white spaces. I tried looking for help and even found a way, but it isn't working.
Here is my code:
#include <string>
#include <ctype.h>
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
int main()
{
int t;
cout << "Enter the number of test cases" << endl;
cin >> t;
do
{
string rawInput;
int isNum, n1, n2, m, t, i, j;
int arr[3];
arr[0] = 0;
arr[1] = 0;
arr[2] = 0;
cout << "Enter a number, or numbers separated by a space, between 1 and 1000." << endl;
while (getline(cin, rawInput))
{
cout << "Rawinput" << rawInput.length();
for (i = 0; i < rawInput.length(); ++i)
{
cout << rawInput[i];
}
int j = 0, k = 0;
for (int j; j < rawInput.length(); ++j)
{
isNum = isdigit(rawInput[j]);
if (isNum)
{
arr[k] = arr[k] * 10 + atoi(rawInput[j]);
}
else
k = k++;
}
cout << "I am Array" << endl;
for (int l = 0; l < 3; l++)
cout << arr[l] << endl;
}
if (arr[0] >= arr[2] && arr[1] >= arr[2])
{
for (int i = 1; i <= arr[2]; i++)
{
if (arr[0] >= i && arr[1] >= i)
{
arr[0] = arr[0] - i;
arr[1] = arr[1] - i;
}
}
}
cout << arr[1] + arr[0];
t--;
} while (t > 0);
}
Specifically the function atoi doesn't seem to work. I tried using stoi, but even that isn't working.
If you are simply trying to collect a series of integers separated by spaces as user input..ie 1 2 3 4 5, you don't have to user the getline method.
You can redo the while loop for a condition like this:
int input;
while (cin >> input)
{
<<HANDLE INPUT>>
}
This is drastically reduce the line parsing you are trying to do and will capture the next inputted integer on that line as long as there is one to take. The loop iteration will go like this with the same series above...
Loop # Input Taken
1 1
2 2
3 3
... ...
This way there is no parsing necessary as it will handle ONE integer input per iteration.
Related
I am trying to write a C++ program to ask user to input a list of 5 numbers and print out the counts of the first number in the input. I have been trying to use array[] but I have some problems. The ideal inputs and outputs are :
Input : 1 1 2 3 1 Output: 3 because there are 3 counts of 1
Input : 1 2 3 4 5 Output: 1
Input : 1 1 1 0 0 Output: 3
Here are my codes, my code allows me to take the inputs but it does not do anything with it. Any help is appreciated!
#include <iostream>
using namespace std;
//frequency function
int frequency(int a[])
{
int count = 0;
for (int i=0; i < 6; i++)
if (a[i] == a[0])
{
count++;
}
cout << count << endl ;
return count;
}
// Driver program
int main() {
int i;
cout << "Please enter your numbers: ";
int a[5] = {a[0],a[1],a[2],a[3],a[4]};
for (i = 1; i < 6; i++)
{
// Reading User Input value Based on index
cin >> a[0] >> a[1] >> a[2]>> a[3] >> a[4];
return 0;
}
int n = sizeof(a)/sizeof(a[0]);
cout << frequency(a);
}
I tried another simpler approach but it needs a little more help
#include <iostream>
#include <string>
using namespace std ;
int main(){
cout << "Please enter your numbers: ";
int a[5];
int repeat;
int first = a[0];
int i;
for (i = 0; i < 6; i++)
{
// Reading User Input value Based on index
cin >> a[i];
}
if (a[i] == first){
repeat++;
}
cout << "Count: " << repeat;
}
you have an odd mixture of techniques for reading a set of numbers. You simply need
cout << "Please enter your numbers: ";
int a[5];
for (int i = 0; i < 5; i++)
{
// Reading User Input value Based on index
cin >> a[i];
}
and you certainly dont want that return as it will end the program
you would be better off using std::vector then you would not need to hard code the array size.
Note at the moment you have 6 as the arrays size in 'frequency' but 5 in main
I want a pattern in which for n=4 in 1st row it has 4 stars and in 2nd row I has 1 space & 3 stars and in 3rd row, it has 2 spaces and 2 stars and so on.
****
***
**
*
The code, I tried to solve this.
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
cout << endl;
int i = 1;
while (i <= n)
{
//Printing Spaces
int space = i - 1;
while (space)
{
cout << " ";
space++;
}
//Printing Stars
int j = 1;
while (j <= n)
{
cout << "*";
j++;
}
cout << endl;
i++;
}
return 0;
}
In your while (space) loop you aren't comparing space to anything, so it assumes that the expression is always true.
Here's a simplified way to do it:
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
cout << endl;
int i = 1;
while (i <= n)
{
// print i-1 spaces
for (int j = i-1; j >= 1; j--)
{
cout << " ";
}
// print n-i+1 stars
for (int j = n; j >= i; j--){
cout << "*";
}
cout << endl;
i++;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
cout << endl;
int i = 1;
while (i <= n)
{
int space = i - 1;
while (space>=1) // change 1.1
{
cout << " ";
space--; // change 1.2
}
int j = i; // change2
while (j <= n)
{
cout << "*";
j++;
}
cout << endl;
i++;
}
return 0;
}
I made only 2 changes in your code so it can work.
1st one
while (space>=1)
what you are doing is you are trying to add space in output so you add space variable in while() loop but that's not going to work because you have to decide first how many spaces you have to print according to that you have to put condition in while() loop. To achieve this space--; added .
For ex. line 4 i=4; space want 3, so space=3; while(space>=1); space--; so while loop runs 3 time and print 3 gaps/spaces.
2nd one
int j = i;
while (j <= n)
if you put j=1; then your gaps print properly but all stars print 4 times as loop runs 4 times always. Due to this condition for i=1; But if you make j=i;
loop runs 4 times for 1st line, 3 times for 2nd line,.....
here is my problem.The program needs to print out the number of numbers in the range between m and n which have different digits.For example:m=97,n=104;output:5.I seem to have problem with comparing the numbers and finding a way to check if there are 2 same digits.Here what I've written up till now:
enter code here
#include <iostream>
using namespace std;
int main()
{
int m, n;
cout << "m=";
cin >> m;
cout << "n=";
cin >> n;
for (int i = m; i <=n; ++i)
{
if (i >= m && i <= n)
{
while (i > 0)
{
m=i%=10;
i/= 10;
cout << m;
}
}
}
system("pause");
return 0;
}
If you can give me the easiest possible solution.Thanks in advance
The best way to do this is to break it down into two parts. The main part is responsible for incrementing the number set (which is easiest if the variables are integers) and the second part is to compare each part of the number to see if a number has been repeated (which is best done as a string).
So first I would define the method responsible for determining if the value has all distinct digits:
bool hasDistinctDigits(int m) {
string number = to_string(m);
for (string::size_type i = 0; i < number.size(); i++) {
for (string::size_type j = i + 1; j < number.size(); j++) {
if (number[i] == number[j]) return false;
}
}
return true;
}
This should be pretty easy to understand. First we need to convert the number to a string. Then we use 2 loops to determine if a character is repeated in a higher index. For example, with the number 102, it will compare the following sets: {1, 0}, {1,2}, {0, 2}
Then in the main function, I would call it like this:
int main()
{
int m, n, matched = 0;
cout << "m=";
cin >> m;
cout << "n=";
cin >> n;
for (int current = m; current <= n; ++current){
if (hasDistinctDigits(current)) {
cout << current << " is distinct" << endl;
matched++;
}
}
cout << "Total Matched: " << matched << endl;
system("pause");
return 0;
}
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;
}
so far this is my code what i am trying to do is say the user inputs 1 2 3 and then presses -1, he or she will be asked to input another set of numbers say 9 8 7, what my programs is suppose to do is display them out as such 1 2 3 9 8 7, but rather it is displaying them like this 6 6 6 6 6 6, basically it counts how many numbers there are and displays that amount of numbers with that number. So can anyone help me out here, how do i make it so that it displays the two sets of numbers combined?
#include <iostream>
#include <vector>
using namespace std;
vector<int> append(vector<int> a, vector<int> b)
{
int n = a.size();
int m = b.size();
vector<int> c(n + m);
int i;
for (i = 0; i < n; i++)
c[i] = a[i];
for (i = 0; i < m; i++)
c[n + i] = b[i];
return c;
}
int main()
{
cout << "Please enter a set of numbers, insert -1 when done.\n";
vector<int>a;
bool more = true;
while (more)
{
int n;
cin >> n;
if (n == -1)
more = false;
else
a.push_back(n);
}
cout << "Please enter another set of numbers, insert -1 when done.\n";
vector<int>b;
more = true;
while (more)
{
int m;
cin >> m;
if (m == -1)
more = false;
else
b.push_back(m);
}
vector<int>d = append(a,b);
{
int i;
for (i= 0; i < d.size(); i++)
cout << d.size() << "\n";
}
}
That's because at the end you're printing the size, not the value:
cout << d.size() << "\n";
Should be:
cout << d[i] << "\n";
It is because when you are printing it, you are printing d.size instead of d[i].
cout << d.size() << "\n";
Would needs to be:
cout << d[i] << endl;