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 1 year ago.
Improve this question
This is the main code:
int main(){
unsigned d=0, x, a[100]={0};
cout << "Input the value of x: ";
cin>>x;
caricaArray(a,x,d);
for(unsigned i=0 ; i<d ;i++)
cout<<a[i]<<endl;
return 0;
}
I need to make a function the inserts in the array all the number between 0 to 100 that are divisible by the integer x.
I tried with my code:
void caricaArray(unsigned a[], unsigned x, unsigned d){
for(int i = 1; i < 100; i++ )
for(int j = 0; j < 100; j++ ){
if( i % x == 0 ){
d++;
a[j] == i;
}
}
}
Is there anyone here has another insight?
thank youu
In other words, you are storing multiples of x into the array, between x and 100.
unsigned int index = 0U;
for (unsigned int i = x; i < 100; i = i + x)
{
a[index++] = i;
}
A number that is evenly divisible by x is a multiple of x.
Edit 1: Division.
If you don't like multiplication, you could use division.
unsigned int array_index = 0U;
for (unsigned int i = 1; i < 100; ++i)
{
if ((i % x) == 0)
{
a[array_index++] = i;
}
}
In both of the above examples, you can replace the array assignment by printing the value.
Related
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 2 years ago.
Improve this question
i'm trying to get the smallest and the biggest number possible by rearanging the 3 digit number.
So i success get the biggest
int maxNumFromNum(int num) {
int freq[10] = {0};
string str = to_string(num);
for (int i = 0; i < str.length(); i++)
freq[str[i] - '0']++;
int res = 0, mul = 1;
for (int i = 0; i <= 9; i++) {
while (freq[i] > 0) {
res = res + (i * mul);
freq[i]--;
mul = mul * 10;
}
}
return res;
}
But now i'm trying to get the smallest one. How can be done that.
Example Outpots:
321 Output> 123, 321
598 Output> 985, 598.
And too, is there more efficiency way to do that?
Thanks in advice.
A simple solution:
#include<bits/stdc++.h>
using namespace std;
int main() {
int n = 231;
string s = to_string(n);
sort(s.begin(), s.end());
cout<<"Smaller: "<<s<<endl;
reverse(s.begin(), s.end());
cout<<"Bigger : "<<s<<endl;
}
It works for integers of any size and runs in O(n lg(n)), where n is the number of digits of the integer.
You could simply change the line:
for (int i = 0; i <= 9; i++) {
to
for (int i = 9; i >= 0; i--) {
void getBigAndSmall(string num, string &outBig, string outSmall){
outBig = 0;
outSmall = 0;
//since you're sorting digits you can use this to sort them with O(n)
//this would be faster than std::sort in this case.
int bigArray[10] = {0};
int smallArray[10] = {0};
for (int i = 0; i < 10; i++){
int digit = (int)num[i] - 48; //this might not be good idea.
bigArray[digit]++;
smallArray[digit]++;
}
int digitsSize = num.size();
for (int i = 0; i < digitsSize; i++){
for (int j = 0; j < 10; j++){
if (bigArray[j] > 0){
bigArray[j]--;
outBig += j;
}
}
for (int j = 9; j > -1; j--){
if (smallArray[j] > 0){
smallArray[j]--;
outSmall += j;
}
}
}
}
This might have some mistakes, since I didn't test it, but it has a runtime O(n).
It gives max integer using input digits without using string
int extractMax(int num) {
std::vector<int> digits;
bool flag = false;
int curDigit;
while (num > 0) {
curDigit = num % 10;
num /= 10;
flag = false;
for (int i = 0; i < digits.size(); ++i) {
if (digits[i] > curDigit) {
flag = true;
digits.insert(digits.begin() + i, curDigit);
break;
}
}
if (not flag) digits.push_back(curDigit);
}
std::sort(digits.begin(), digits.end());
int dec = 1, int_val = 0;
for (auto& it : digits){
int_val += it * dec;
dec *= 10;
}
return int_val;
}
If you want to get smallest integer, change this line:
std::sort(digits.begin(), digits.end());
With this:
std::sort(digits.begin(), digits.end(), greater<int>());
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
So I have to make a program in c++ that returns the average of all the numbers in a 2d array, and it gets one case right. When the dimensions are 1 and 1, it returns 7 when the seed is 75. But any other case outputs 0. Is the seed statement wrong?
#include <iostream>
#include <ctime>
using namespace std;
const int NUM_ROWS = 7;
const int NUM_COLS = 9;
double getArrayAverage(int myArray[NUM_ROWS][NUM_COLS], int rows, int cols);
int main() {
const int NUM_ROWS = 7;
const int NUM_COLS = 9;
int seed;
int array[NUM_ROWS][NUM_COLS];
double num = 0;
cin >> seed;
srand(seed);
for (int i = 0; i < NUM_ROWS; i++) {
for (int j = 0; i < NUM_COLS; i++) {
array[i][j] = rand() % 10 + 1;
}
}
num = getArrayAverage(array, NUM_ROWS, NUM_COLS);
cout << num << endl;
return 0;
}
double getArrayAverage(int array[NUM_ROWS][NUM_COLS], int rows, int cols) {
int sum = 0;
double average = 0.0;
for (int i = 0; i < rows; i++) {
for (int j = 0; i < cols; i++) {
sum = sum + array[i][j];
}
}
average = sum / (rows * cols);
return average;
}
Three errors that I can quickly see
for (int i = 0; i < NUM_ROWS; i++) {
for (int j = 0; i < NUM_COLS; i++) {
should be
for (int i = 0; i < NUM_ROWS; i++) {
for (int j = 0; j < NUM_COLS; j++) {
This error happens twice, be very careful when cutting and pasting code!
The other error is using integer division when you clearly want floating point division. In this statement
average = sum / (rows * cols);
sum and (rows * cols) are both int values. So the division will be an integer division. In integer division the result is the integer obtained by truncating the result towrds zero. So 1/2 is 0, 8/3 is 2, 14/4 is 3 etc. In other words you lose the fractional part of the result in integer division.
To get floating point division and keep the fractions cast one of the int values to a double, e.g.
average = (double)sum / (rows * cols);
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 4 years ago.
Improve this question
How to dynamically fill the following output in a two-dimensional array with the same number of rows and columns.
Edit: Here is the solution in c++ using #TheGeneral code from c#.
#include <iostream>
using namespace std;
int main(){
int size = 10;
int half = size/2;
int matrix[size][size];
int number1 = 0;
int number2 = 0;
for(int i = 1; i<=size; i++){
for(int j = 1; j<= size; j++){
if(i > half){
number1 = size + 1 - i;
}else{
number1 = i;
}
if(j > half){
number2 = size + 1 - j;
}else{
number2 = j;
}
if(number1 < number2){
matrix[i-1][j-1] = number1;
}else{
matrix[i-1][j-1] = number2;
}
}
}
cout<<"MATRIX:"<<endl;
for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
cout<<"["<<matrix[i][j]<<"] \t";
}
cout<<endl;
}
return 0;
}
For a bit of fun
The only note worthy things going on, are
Starting the index from 1 (makes things easier)
Conditional Operator to say if an index is greater than 5 reverse the count
Math.Min to make it syemtrical
Exmaple
private static int[, ] CreateArray()
{
var ary = new int[10, 10];
for (var i = 1; i <= 10; i++)
for (var j = 1; j <= 10; j++)
ary[i - 1, j - 1] = Math.Min(i > 5 ? 11 - i : i, j > 5 ? 11 - j : j);
return ary;
}
Demo here
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 have written a code to find the largest palindrome formed by multiplication of two 3-digit numbers. However instead of just getting the desired answer i.e. the largest palindrome, I am getting the list of all possible palindromes. How do I program it to find the largest.
Code:
#include <iostream>
using namespace std;
int revfunc(int x) {
int rev = 0, num, d;
num = x;
while(num != 0) {
d = num % 10;
rev = (rev * 10) + d;
num = num / 10;
}
long int maxi = 0;
if(x == rev && maxi < x) {
maxi=x;
cout<<maxi<<endl;
}
}
int main() {
long int ans;
for(int i = 100; i <= 999; i++) {
for(int j = 100; j <= 999; j++) {
ans = i * j;
revfunc(ans);
}
}
cin.get();
return 0;
}
In your program you don't actually select the maximum palindrome, you just dump them all. Here is minimal correction for code to work:
bool revfunc(int x){
int rev = 0, num, d;
num = x;
while (num != 0){
d = num % 10;
rev = (rev * 10) + d;
num = num / 10;
}
long int maxi = 0;
return x == rev&&maxi < x;
}
int main()
{
int max_palindrome = 0;
long int ans;
for (int i = 100; i <= 999; i++){
for (int j = 100; j <= 999; j++){
ans = i*j;
if (ans > max_palindrome && revfunc(ans))
{
max_palindrome = ans;
}
}
}
cout << max_palindrome;
cin.get();
return 0;
}
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 7 years ago.
Improve this question
I need to find the average of the negative elements in the two-dimensional array. This is what I've got so far:
#include <iostream>
using namespace std;
int main() {
int A[3][3];
int i, j;
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
cout << "\n A[" << i + 1 << "][" << j + 1 <<"]=";
cin >> A[i][j];
}
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++)
cout << A[i][j] << "\t";
cout << "\n";
}
}
You could do it for example:
int negNumber = 0;
int sum = 0;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (A[i][j] < 0) {
++negNumber; // increase negatives numbers found
sum += A[i][j]; // add to our result the number
}
}
}
sum = negNumber > 0 ? sum / negNumber : sum; // we need to check if we found at least one negative number
I hope it will help you but be careful! It will return a truncated value (an int).
the ternary operation could be difficult to undertand so you can do it:
if (negNumber > 0) {
sum /= negNumber;
}