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
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 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.
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 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;
}
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
Here is my C++ code for counting sort algorithm , there is no errors as well as no warnings but when I want to execute it it's give me "Counting.exe has stopped working" I think it is a run time error.
void Counting_sort()
{
int A[]={5,15,20,30,40,8,36,25,96,15,40,15,96,47,20};
int k = 15 ;
int n = 15;
int i, j;
int B[15];
int C[100];
for(i = 0; i <= k; i++)
C[i] = 0;
for(j =1; j <= n; j++)
C[A[j]] = C[A[j]] + 1;
for(i = 1; i <= k; i++)
C[i] = C[i] + C[i-1];
for(j = n; j >= 1; j--)
{
B[C[A[j]]] = A[j];
C[A[j]] = C[A[j]] - 1;
}
cout << "\nThe Sorted array is : ";
for(i = 1; i <= n; i++)
cout << B[i] << " " ;
}
void main()
{
Counting_sort();
}
for(j = n; j >= 1; j--)
{
// You are accessing A[j]
}
So A[15] is a invalid access and will lead to undefined behavior.
The valid access for array A[15] is A[0] to A[14] anything other than this is array out of bound access.
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 8 years ago.
Improve this question
I have an array that keeps the number of each element.
int total[5] = {2,3,4,5,6}
int num = 5; //array total has 5 elements
This means that we have 2 element 0's, 3 element 1's in our original array. We are not worried about the original array since I already have a code to keep the number of the elements.
I need a nested for loop that creates a new array that looks like this:
array[0] = 1;
array[1] = 1;
array[2] = 5;
array[3] = 5;
array[4] = 5;
array[5] = 9;
array[6] = 9;
array[7] = 9;
array[8] = 9;
and so on. That is, we store a value in our new array as many as the its value in "total" array. Values 1,5,9, etc. are stored in an array called element. I have something like this so far:
for (int i = 0; i < num; i++){
for (int j = 0; j < total[i]; j++){
array[i + j] = element[i];
}
}
Can somebody help me to figure this out?
An easy solution (though not necessarily elegant) is to do the following:
int count = 0;
for (int i = 0; i < num; i++){
for (int j = 0; j < total[i]; j++){
array[count] = element[i];
count++;
}
}
then you don't need to worry about trying to figure out what position you are at for the array.
You need to track the number of the elements:
int sum = 0;
for (int i = 0; i < num; i++){
for (int j = 0; j < total[i]; j++){
array[sum + j] = element[i];
}
sum += total[i];
}