how to compare unknown elements of an array in c++? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 26 days ago.
Improve this question
Sorry for bad English .
I was trying to write a program that gets a number and see if the digits of an entered number are repeated or not . I did try to if(analyse[0]==analyse[1]==analyse[2]==...) but since I don't know exactly how many elements will array have, it didn't work
#include<iostream>
int main(){
int number,number_help;
const int count{10};
std::cin>>number;
number_help = number ;
int digitcount{0};
while(number_help>0){
number_help/=10;
digitcount+=1;
}
int analyse[count]{};
for(size_t i {0}; i<digitcount ; i++){
analyse[i] = number%10;
number/=10;
}
//I don't know what to code here
return 0;
}

Change your approach: count how many there are of each digit instead of comparing them to each other.
This is much simpler.
Example:
#include<iostream>
int main(){
int number;
std::cin >> number;
const int count{10};
int frequency[count]{};
do {
frequency[number % 10] += 1;
number /= 10;
} while (number != 0);
for (int i{0}; i < count; i++) {
if (frequency[i] > 1) {
std::cout << i << " was repeated " << frequency[i] << " times.\n";
}
}
}

Related

what will bw the output of this code? c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
#include <iostream>
using namespace std;
int f(int i){
int k=0;
if(i>0)
{
int k=i*10;
}
else {
int k= i++;
}
cout <<k;
return i;
}
int main()
{
cout << f(1);
cout << ".";
cout << f(0);
return 0;
}
This is the code, compiler shows "01.01" which i quite don't understand, any help will be very much welcomed!
int k = i * 10; and int k = i++; are declarations of k that shadow the outer k. The statement std::cout << k; in the outer scope therefore always outputs zero.
The only effect of the if body is to increase i by 1. And it only does that if i is zero (or less). That value of i is returned printed.
Thus the output is 01.01. Armed with a line by line debugger, the shadowing effect will be obvious.

Problem with calculating Primes within a given Range in C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Im trying to create a program that does various math operations, and i wanted to start with calculating prime numbers within a given range. However, when i try to execute the code, it just returns exit status -1. What is wrong with the program and how do i fix it?
#include <iostream>
#include <vector>
using namespace std;
void getPrimes(int min, int max) {
int range = max - min;
std::vector< int > possible_values;
for (int q = 0; q < range; q++) {
possible_values.push_back(min + q);
}
for (int i = 0; i < range; i++) {
int num_of_factors = 0;
int num = possible_values.at(i);
for (int c = 0; c < num; c++) {
if (num % c == 0) {
num_of_factors++;
}
}
if (num_of_factors == 0) {
std::cout << num << endl;
}
}
}
int main() {
int min, max;
std::cout << "min: ";
std::cin >> min;
std::cout << "max: ";
std::cin >> max;
getPrimes(min, max);
}
this loop should start from 2 because :
c%0 is undefined behavior
every number %1 is 0 so you can see why num_of_factors is never 0
for (int c = 2; c < num; c++) {
if (num % c == 0) {
num_of_factors++;
}
}

Skipped condition and I don't know why [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I was debugging this, and debugger skipped the last 'if' even 'sum' was equal to 'n' and jump straight to 'else', I don't know why. Please help.
P/s: Can I use dynamic array to increase the mobility of my program?
#include <iostream>
#include <math.h>
using namespace std;
int exponent_of_10(); // set position for digits
int exponent_of_10(int a, int b){
for(int j = b; j>0;j--)
{
a *= 10;
}
return a;
}
main() //check if the number was palindromic
{
int n;
int a[6]={0,0,0,0,0,0};
int i = 0;
int temp;
int S;
cout<< "Input n (maximum of 6 digits): ";
cin>> n;
do
{
if(n<1)
{break;}
temp=n%10;
a[i]=temp;
n=(n-temp)/10;
i++;
}
while (n!=0);
int sum = 0;
for(int j=0; j<=5; j++)
{
exponent_of_10(a[j],j);
S = exponent_of_10(a[j],j);
if (S==0)
{break;}
sum +=S;
}
if(sum==n)
{
cout<< "Congratz, this is PALIDROMIC NUMBER !!";
}
else
cout<< "Sad life, this is NOT palidromic number";
return 0;
}
When the code exits the do ... while() loop, n is 0. For the test in the if to be meaningful, the code should save the original value of n somewhere and compare sum to that original value.

c++: I want to make a arithmetic progression using array, but it gives me messy code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
#include<iostream>
using namespace std;
//prototype
void fillArray(int[], int, int);
void printArray(int[], int);
int main()
{
srand(time(0)); // initialize random number generator
const int size = 5, SIZE=10;
int ar1[size], ar2[size],inc;
cout << "Enter the first value and increment for ar1: ";
cin >> ar1[0] >> inc;
cout << "Enter the first value and increment for ar2: ";
cin >> ar2[0] >> inc;
cout << "(1) ar1:\n";
fillArray(ar1, size, inc);
printArray(ar1, size);
system("pause");
return 0;
}
void fillArray(int ar[], int size, int inc)
{
for (int i = 1; i < size; i++)
{
ar[i] = ar[0] + inc;
cout << ar[size];
}
}
void printArray(int ar[], int size)
{
for (int i = 0; i < size; i++) {
cout << ar[i] << ' ';
}
cout << endl;
}
Ask the user to input ar1[0] and inc.the first one ar1[0] is settled by user input, and the rest is increased by inc.So if I enter 4 3 ,it should return 4 7 10 13 16. My outcome is like this:
I know there is something wrong with the function fillArray but I don't know how to fix it. Can anyone tell me the solution? Thanks
Yes there's something wrong with your function fillArray. It assumes that the first element is initialized (which is, indeed) but then doesn't initialize the other elements in ascending order, because you always add inc to the value of the first element. Moreover it prints out ar[size] which is out of bounds at every step (the first strange numbers in your output). You can fix with this:
void fillArray(int ar[], int size, int inc)
{
for (int i = 1; i < size; i++)
{
ar[i] = ar[i-1] + inc;
}
}
Or you can decide to pass the first value as a parameter too.
I assume you wanted to use size, even if you also declare another similar variable named SIZE with a different value. You should also consider using std::vectors instead of arrays as a safer a more practical option.
Finally, in your code you ask the user to input inc twice (overwriting it) before using it.

Project Euler task #8, code starts returning wrong answers after certain point [closed]

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 having some sort of a problem in a task from
https://projecteuler.net/problem=8, (finding highest product of 13 consecutive numbers from a 1000-number string) where up to some point the program gives me predictable results and then the function returns a number very close to the unsigned long long int range. The point where it occurs depends on the values which were read, for instance if the string of numbers consisted mostly of 8s and 9s, it would happen sooner than it would if it had only 5s and 6s. Why does it happen?
#include <iostream>
#include <fstream>
using namespace std;
int product (int res, int a, char buffer[]){
for (int i = 0; i < a; i++){
//simple char to int conversion
res*=(buffer[i] - '0');
}
return res;
}
int main () {
char check;
int res = 1;
fstream plik;
plik.open ("8.txt");
unsigned long long int high;
unsigned long long int result;
//main function in the program
if (plik.good()){
char buffer [13];
for (int i = 0; i < 13; i++){
plik >> buffer[i];
}
result = product (res, 13, buffer);
high = result;
cout << high << endl;
//the main checking loop
while (!plik.eof()){
//just an interruption to make it possible to view consecutive products
//the iteration in the buffer
for (int i = 0; i < 12; i++){
buffer[i] = buffer[i+1];
}
plik >> buffer[12];
result = product (res, 13, buffer);
//comparison between the current product and highest one
if (high < result){
high = result;
}
cin >> check;
cout << high << endl;
//again a tool for checking where the problem arises
for (int i = 0; i < 13; i++){
cout << buffer[i] << " ";
}
cout << endl;
}
plik.close();
cout << high << endl;
}
return 0;
}
The program prints out the currently highest product and all the numbers currently contained in the array.
It looks like this:
The error
Use unsigned long long int instead of int to calculate the product. The product of 13 digits can easily become larger than the largest int.