Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to build a program that reads numbers from "bac.txt" and returns the 2 digit number/numbers (10,11,12,...,99) that appear most frequently. For example if the file "bac.txt" contains 393 17775787 72194942 12121774 it will return 77 and 21. I have managed to build a working function counter(int n) that can count how many times n is found in the file and returns i which is the number of times n has been found. Now I can't seem to find a way to print to the screen the number/numbers that are found most often. I tried to use some kind of for loop but it doesn't work.
Here is my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int counter(int n){
int i=0,j=0;
char x1=n/10+'0';
char x2=n%10+'0';
char a;
char b=NULL;
fstream fisier("bac.txt", fstream::in);
while (fisier >> noskipws >> a) {
if(b==x1&&a==x2)
i++;
b=a;
}
return i;
}
int main()
{
int v[90];
int v1[90];
int i,maxim=0,nr;
for(i=10;i<100;i++)
{
v[i]=counter(i);
if(v[i]>maxim)maxim=v[i];
}
for(i=10;i<100;i++)
if(v[i]==maxim)
cout<<i;
}
I have corrected the code.It works now.
You may check the changes.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int counter(int n)
{
int i = 0, j = 0;
char x1 = n / 10 + '0';
char x2 = n % 10 + '0';
char a;
char b;
fstream fisier("bac.txt", fstream::in);
fisier >> b;
while (fisier >> a) {
if (b == x1 && a == x2)
i++;
b = a;
}
return i;
}
int main()
{
int v[101];
int i, maxim = 0, nr;
for (i = 10; i < 100; i++) {
v[i] = counter(i);
if (v[i] > maxim)
maxim = v[i];
}
for (i = 10; i < 100; i++)
if (v[i] == maxim)
cout << i;
}
i can't say you why your program's not working, but i can say you how i'd solve it. you have to store in an int vector the 2 digit number that you read from file (ex. if file contains 1234 the int vector will contains 12 23 34) than you have to find the 2 number that appear most times, so, you have read the first vector element and store it in a variable, than you count how many times it appear in the vector and you save the number you were searching in max_a and the times it appear in max_times_a (remember that when you find in the vector the number you're searching you have to put a -1 on it) than you search in the vector another number and count how many times it appear (putting -1 on that number) than you store that number in max_b and the times it appears in max_times_b. than what you have to do is to slide the vector and if you reach the end becouse you only read -1 you have max_a and max_b as result, but if you find a number you have to count how many times it appear and checking if it appear most times of max_a or max_b, and in case it appear most times you have to swap the values. i hope to have been useful to you.
Tommaso
Related
The problem is to take 3 inputs:
1) No. of test cases
2) No. of digits in a number
3) N space separated digit numbers
And to output :
1) No. of sets
2) No. of combinations in each set
I want to print those outputs but No of combination output is returning zero on each and every set
I've already tried troubleshooting and debugging the problem, but none of those worked....
/* Read input from STDIN. Print your output to STDOUT*/
#include<iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
using namespace std;
int factorial (int count);
int main(int argc, char *a[])
{
//intialize variables
int i,T,b,S[i],N,NN[i],C[i],count=0;
cin >> T;
while(T>0) {
cin >> N;
for(i=0;i<N;i++) {
cin >> NN[i];
if(i<N-1) {
S[i] = (N-i);// S[i] is Category 02
count++;
}//end of if
}//end of for loop
for(int j=0;j<N;j++) {
C[i] = factorial(count)/(factorial(i)*factorial(count - i));//
}//end of for loop
cout <<"No. of sets =" <<count++<<endl;
for(int k=0;k<N;k++) {
cout<<"No.of combinations on each set :";
cout<<C[i]<<endl;
} // end fo for loop
}//end of while loop
return 0;
}//end of main
int factorial(int count)
{
int i;
for(i = count-1; i > 1; i--)
count *= i;
return count ;
}//end of function
THIS OUTPUT IS COMING:
"No. of combinations on set 0 : 0"
"No. of combinations on set 1 : 0"
………….
Well it's gone wrong already here
//intialize variables
int i,T,b,S[i],N,NN[i],C[i],count=0;
What's the value of i here? Answer, it doesn't have one. If i doesn't have a value then what's the size of this array NN[i]? Answer, who knows.
When you declare an array in C++, you must give it a size. The size cannot be a variable, it must be a cosntant. And it especialy cannot be a variable without a value.
Your program has undefined behaviour.
EDIT - this would be an improvement
#include <vector>
int main()
{
int T;
cin >> T;
while (T > 0)
{
int N;
cin >> N;
std::vector<int> NN(N), S(N);
for (int i = 0; i < N; i++)
{
...
First improvement is that I using a std::vector instead of an array. Unlike arrays vectors can have variable sizes. Second improvement is that I only declare variables when I need them, I don't declare all the variables at the start of the function. So I only declare NN and S when I know what the value of N is, so I know how big the vectors need to be.
This question already has answers here:
c++ : dynamic number of nested for loops (without recursion)
(3 answers)
Closed 6 years ago.
I have read the following questions but have not found a solution to my problem:
c++ : dynamic number of nested for loops (without recursion)
variable nested for loops
actual problem statement is :-
Job and Jimmy both are playing with numbers. Job gives Jimmy an array of numbers and asks him to tell the minimum possible last number of a non decreasing sequence of length L.
Input Format
First input line consists of a number which is size of array N.
Next line contains N space separated elements of array.
Next line contains length of the non decreasing sequence i.e. L.
Output Format
You have to print the minimum possible last number of a sequence and if their is no non-decreasing sequence of length L, then print -1
Sample Input
7
9 7 2 5 4 11 12
3
Sample Output
11
Explanation
In sample input, possible non-decreasing sequences of length L=3 are (9,11,12) , (7,11,12) , (2,5,11) , (2,4,11) , (2,5,12) , (2,4,12) , (2,11,12) , (5,11,12) , (4,11,12) and the minimum last number is 11 for the sequences (2,5,11) and (2,4,11). Hence, the answer is 11."
my code...
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int fact(int y,int x)
{
static int temp=0;
if(temp==x)
{
temp=0;
return 1;
}
else
{
++temp;
return y*fact((y-1),x);
}
}
int main() {
int num,randmax,n,s,q,w last=-1, minlast=-1;
cin>>n;
vector<int> a(n);
for(int i=0;i<n; i++)
{
cin>>a[i];
}
cin>>s;
vector<vector<int>> c;
q=fact(s);
c.resize(q);
for(int i = 0 ; i < q ; ++i)
{
//Grow Columns by n
a[i].resize(s);
}
w=q;
randmax=n-1;
int k=0;
while(w)
{
for(int i=0 ; i<n ; i++){
}
num=rand()%randmax; // this works perfect as expected
c[][i]=a[num];
}
w--;
}
/*for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
for(int k=j+1;k<n; k++)
{
if((a[i]<=a[j])&&(a[j]<=a[k]))
{
last=a[k];
if(minlast=-1)
{
minlast=a[k];
}
if(last<minlast){
minlast=last;
}
}
}
}
}
*/
cout<<last;
return 0;
}
`
I would tell you what I tried to do... I thought of mapping the data by having them randomly assigned in one of my array and then computing them..
I got lost somewhere in my code...plz gimme an solution to it...and more imp. a good explanation of the same as I got stuck at times when I need a dynamic nested n loop type of thing...
also it would be more helpful if you edit in my code or algo so that I could learn where my mistakes are there...
Thanks in advance for you time...
As the answers your links have pointed out, you can imitate a dynamic amount of for loops by using an array David gives a fine implementation here.
For the actual problem you gave though, I see no need for a dynamic amount of for loops at all. The problem is a standard non-decreasing subsequence problem with a slight variation.
#include <iostream>
#include <fstream>
int N, L;
int arr[100], seq[100];
int bst;
int main() {
std::ifstream file ("c.txt");
file >> N;
for(int n = 0; n < N; ++n)
file >> arr[n];
file >> L;
file.close();
bst = 1e9;
for(int i = 0; i <= N; ++i) seq[i] = 1e9;
for(int i = 0; i < N; ++i)
{
int x = 0;
while(seq[x] < arr[i]) ++x;
seq[x] = arr[i];
if(x + 1 >= L)
bst = std::min(bst, arr[i]);
}
std::cout << bst << std::endl;
return 0;
}
This code should solve your problem. The first part does standard parsing and initialization. The rest is a variation on the LIS problem, which has several standard algorithms that solve it. Here, we just check that whenever we extend an array of length L or longer, we see if the element is smaller than our current.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to solve algorithmic problems on the UVa online judge but I am stuck on the 3n+1 problem. I see the right output every time, but the judge says it is a wrong answer. Why?
Also, how can I optimize this code so that it does not take a long time for 1000000?
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main(){
int a;
int b;
int cyclelength(int i);
while (scanf("%d %d\n",&a,&b)==2){
int max = 0;
if (b < a){
for (int i = b; i < a; i++){
if (cyclelength(i) > max)
max = cyclelength(i);
}
}
else
{
for (int i = a; i < b; i++){
if (cyclelength(i) > max)
max = cyclelength(i);
}
}
cout << a << " " << b << " " << max << endl;
}
}
int cyclelength(int i){
if(i==1)
return 1;
if(!(i%2))
return cyclelength(i/2)+1;
else
return cyclelength(3*i+1)+1;
}
Your submission is incorrect because you aren't iterating over the range from a to b, inclusive.
The problem statement reads:
For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j.
Your code loops over
for (int i = b; i < a; i++)
and
for (int i = a; i < b; i++)
thus omitting a in the former case and b in the latter.
You should change the loops to
for (int i = b; i <= a; i++)
and
for (int i = a; i <= b; i++)
You can speed up your code somewhat by memoizing the results of cyclelength. This lets you look up previously computed values instead of spending time on computing them again.
Here is a memoized version of cyclelength:
#include <map>
using namespace std;
map<long long, int> memo;
int cyclelength(long long i) {
if (i == 1) {
return 1;
}
if (memo.count(i) == 1) {
return memo[i];
}
if (i%2 == 0) {
return memo[i] = 1 + cyclelength(i/2);
} else {
return memo[i] = 1 + cyclelength(3*i + 1);
}
}
An unordered_map would be faster, but the UVA judge returned a compilation error for #include <unordered_map>.
Note that my version of cyclelength takes a long long argument. That lets me calculate cyclelength(999999), which eventually causes an integer overflow due to repeated 3n+1 operations if you limit yourself to 32-bit integer values. The problem statement promises that "no operation overflows a 32-bit integer" and you can get your submission accepted if you only use int values, but you have to use a larger integer type if you want to compute cyclelength for values in the range [1, 1000000).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have this code. What i wanted to do is to replace every letter from every char with indicated number. Like for A is 10, and so on until J, 19. My code works well if i have only one letter in my char array, but if i have more after another it copies useless things. I think that something is wrong with strncat.
#include<conio.h>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char litera[11]={"ABCDEFGHIJ"};
char cifra[11]={"0123456789"};
char rezultat[256]={0};
int n;cin>>n;cin.get();
for(int i=0;i<n;i++)
{
char x[256];
cin.get(x,256);cin.get();
int k=strlen(x);
for(int j=0;j<k;j++)
{
int p = strchr(litera,x[j])-litera;
if(p>=0 )
{
strncat(rezultat, x,j);
// my p is the indicator for letter and number
strcat(rezultat,"1");
// I'm copying an 1 there because i have numbers
// between 10-19 and in my int array i have only
// the second cipher
strcpy(x,x+j);
rezultat[strlen(rezultat)]=cifra[p];
}
}
cout<<rezultat<<endl;
memset(rezultat,0,sizeof(rezultat));
}
getch();
return 0;
}
Input: 07B, 1AA, C8A
Output: 0711, 11010, 12810
My output:
0711
110AA1
12C810
If you guys can tell me where the problem is, you'll help me a lot, every suggestion is well received, even if is not about this problem. Thanks!
If it is allowed to use vector and string then you can try the following (I tested it and it works.) The input here should be line by line(each line is a new string to be converted) but of course you may modify this part based on your input format:
Include those:
#include <vector>
#include <iostream>
#include <string>
//#include <conio.h>
//#include <cstring>
//using namespace std;
int main()
{
int n;
std::cin >> n;
while (n--)
{
string input;
std::cin >> input;
vector<char> resultat;
for (int i = 0; i < input.size(); i++)
{
if (input.at(i) >= 'A' && input.at(i) <= 'J') // it is a letter between A and J
{
int number = input.at(i) - 'A' + 10;
char ones = (char)(((int)'0') + (number % 10));
char tens = (char)(((int)'0') + (number / 10));
resultat.push_back(tens);
resultat.push_back(ones);
}
else
resultat.push_back(input.at(i));
}
for (int i = 0; i < resultat.size(); i++)
{
std::cout << resultat[i];
}
std::cout << endl;
}
}
if string is not allowed just use your character array. There seems to be nothing wrong in your input format.
If vector is not allowed either, then you may create a char pointer the initialize it based on the final size.
For example:
int newSize=0;
for (int i = 0; i < input.size(); i++)
{
if (input.at(i) >= 'A' && input.at(i) <= 'J')
newSize+=2;
else
newSize++;
}
char* resultat = new char[newSize];
...
Then just fill the resultat.
Hope that helps!
Do you have to do it with your multiple arrays? You could perform a switch-case for only specified inputs. Since you are replacing letters A-J with numbers 10-19, you can easily implement this in switch case. You can also check for incorrect input as well.
If you have to use arrays, first question is why is you numerical array the single digits? Is this is constraint? Cant you use :
char cifra[11]={"10","11","12","13","14","15","16","17","18","19"};
for the array instead, that way you can refer to the array index and replace it using the index? This way, you can compliment the arrays by just using index referencing to replace the output based on the index, such as A is index 1 for alphabetical array which refers to index 1 of the numerical array which is "10" so you just print the array index value to a variable and output it.
Here's a link to the problem I'm trying to solve: https://projecteuler.net/problem=8.
I've written a code that seems to work well while I'm calculating a product of anything from 1 to 12 (included) consecutive digits. For example the biggest product of 12 adjacent digits I get is 1792336896, which seems logical as it's less than 9^12.
However, when I put 13 instead of 12 in my code, the answer I get is 18446744073195294960 which is way out of proportion. I've been looking at this a couple of days now, and I just can't see where I went wrong. I would really appreciate if anyone could look into it.
Here's my code:
#include <iostream>
#include <fstream>
using namespace std;
int numbers[1000];
string line;
string numb;
uint64_t product=0;
void convert(){
for (int i = 0 ; i < numb.length() ; i++)
{
numbers[i] = numb[i] - '0';
}
}
void calculate_lines(){
int digits = 13;
for (int i=0;i<numb.length()-digits;i++){
int temp=1;
for (int j=i;j<digits+i;j++){
if (numbers[j] == 0){
i+=digits;
break;
}
temp=temp*numbers[j];
}
if (temp>=product){
product=temp;
}
}
}
void read_lines(){
ifstream infile;
infile.open("numbers.txt");
if (infile.is_open())
{
while (getline(infile,line))
{
numb+=line;
}
infile.close();
}
}
int main()
{
read_lines();
convert();
calculate_lines();
cout << product << endl;
return 0;
}
You calculate the product with the variable int temp. This isn't large enough to contain a product of 13 digits, so it overflows. It becomes a negative value, which later becomes a very large positive value when converted to uint64_t.
While the variable that holds the final result product is a uint64_t, you need to make sure that intermediate values are stored in large enough variables. You need temp to be uint64_t as well.