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);
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.
Beginner here...
Doing a code to check language performance in Cpp, Java and Python.
The code must generate a random number N (1-60), fill a NxN matrix with random numbers between 0 and 9 and calculate its determinant.
I started with cpp, but sometimes it succeeds, sometimes it fails. My guess is that crashes are related to bigger than "long long int" numbers. Can you guys please check my code?
GNU GCC / CodeBlocks.
Thanks,
Guile.
#include <iostream>
#include <time.h>
using namespace std;
struct Mat{
int N = 2;
int mat[60][60];
long long int det;
};
void setSize(Mat *Size){
srand (time(NULL));
do {
Size->N = rand()%60;
}
while (Size->N < 1);
}
void setMatrix (Mat *mat){
srand (time(NULL));
for (int i = 0; i < mat->N; i++){
for (int j = 0; j < mat->N; j++){
mat->mat[i][j] = rand()%10;
}
}
}
void det(Mat *m1){
int i, j, k;
long long int Ratio;
long long int determinant;
for(i = 0; i < m1->N; i++){
for(j = 0; j < m1->N; j++){
if(j>i){
Ratio = m1->mat[j][i]/m1->mat[i][i];
for(k = 0; k < m1->N; k++){
m1->mat[j][k] -= Ratio * m1->mat[i][k];
}
}
}
}
determinant = 1;
for(i = 0; i < m1->N; i++)
determinant *= m1->mat[i][i];
m1->det = determinant;
}
int main (void){
Mat M1;
setSize(&M1);
setMatrix (&M1);
det(&M1);
cout<<"Matrix size: "<<M1.N<<endl;
cout<<"Matrix determinant: "<<M1.det<<"\n\n";
return 0;
}
Inside your function setMatrix:
for (int j = 0; j < mat->N; j++){
mat->mat[i][j] = rand()%10;
}
rand() % 10 can sometimes generate 0 as well, thus putting zeroes inside your matrix. This will cause a floating-point exception when you do:
Ratio = m1->mat[j][i]/m1->mat[i][i];
This line will again cause an issue when you do
m1->mat[j][k] -= Ratio * m1->mat[i][k];
which can again set a zero in your matrix at mat[j][k] later which can become the denominator again, causing the floating-point exception.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
int ma(float array[], int N)
{
int k = 0;
float max = array[k];
for (int i = 0; i < N; ++i) {
if (array[i] > max) {
max = array[i];
k = i;
}
}
return k;
}
int main()
{
int t;
while (t--) {
int n;
cin >> n;
int w[n], p[n];
for (int i = 0; i < n; i++)
cin >> w[i];
for (int i = 0; i < n; i++)
cin >> p[i];
float x[n];
for (int i = 0; i < n; i++)
x[i] = p[i] / w[i];
int weigth = 0, profit = 0;
while (weigth <= 20) {
// int k=distance(x, max_element(x, x + n));
// int k= std::distance(x, max_element(x, x + sizeof(x)/sizeof(x)));
int k = ma(x, n);
weigth = weigth + w[k];
profit = profit + p[k];
x[k] = p[k] = w[k] = 0;
}
cout << weigth << endl
<< profit << endl;
}
}
The above code is not printing anything. If you want the question refer to "catch-the-match":
your code is not even compiling,
you can not do this in C++
int n;
cin >> n;
int w[n], p[n];
because n must be a constant at compiling time, on the other hand doing this:
int t;
while (t--) {
is producing an unpredictable number of iterations in the loop since t is not initialized
You have to declare int n as a const int n in order for you code to compile correctly.
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 5 years ago.
Improve this question
here I wrote a code which sorts arrays but something is wrong and I dont understand what is wrong please explain me..
#include <iostream>
using namespace std;
void printArray(int array[], int length){
for (int i = 0; i < length; i++)
cout << array[i];
cout << endl;
}
/* min and max values which are the first and the last elements of the array*/
void countingSort(int array[], int length){
int max = array[0];
int min = array[0];
int count[max - min + 1] = {};
int sum = 0;
for (int i = 0; i < length; i++){
if (array[i] > max)
max = array[i];
else
min = array[i];
}
/* the new array with indexes*/
for (int i = 0; i < max - min + 1; i++){
count[array[i]]++;
}
for (int j = 0;j < max - min;j++)
{
while(count[j] --)
array[sum++] = j;
}
}
int main() {
int array[] = {4,7,8,9,10,6};
countingSort(array, 6);
printArray(array,6);
return 0;
}
it have to sort the arrays through indexes
int max = array[0];
This sets max to the first element of the array, not the last one. So when this line of code runs:
int count[max - min + 1] = {};
count always has a size of 1.
Change it to:
int max = array[length - 1];
Also, C++ does not support Variable Length Arrays; there are only some compilers that have extended their support for it. The size of the array must be a fixed constant at the time of compilation for these compilers. If you want to use VLAs, use std::vector instead.
The function countingSort has around 50% lines with bugs.
Your count array always has exactly one (array[0] - array[0] + 1) element.
This means that you have undefined behaviour when reading or writing beyond index 0.
(Variable-length arrays are a non-standard extension. Learn how to use std::vector.)
You don't know the maximum and minimum values until after the loop that determines what they are has completed.
Move the array declaration after that loop.
Also, you need to fix that loop, because it doesn't necessarily find the minimum value (array[i] <= max does not mean that you have found one).
You will have min == 6 for your example input, for instance.
The counting loop needs to be
for (int i = 0; i < length; i++)
because you're looping over array, which has length elements.
And you need to adjust your counter, since the minimum value corresponds to index 0 but isn't necessarily 0:
count[array[i] - min]++;
And the "result-gathering" loop should be
for (int j = 0;j < max - min + 1;j++)
because count has max - min + 1 elements, and you need to
array[sum++] = min + j;
because the first element counts the min occurrences, not the zeroes.
I think that covers most of your problems.
(And a side note: "sum" is a very peculiar name for something that isn't a sum.)
#include <iostream>
using namespace std;
void printArray(int array[], int length){
for (int i = 0; i < length; i++)
cout << array[i];
cout << endl;
}
void countingSort(int array[], int length){
int max = array[length - 1];
int min = array[0];
int count[100] = {};
int sum = 0;
for (int i = 0; i < length; i++){
if( array[i] > max)
max = array[i];
else
min = array[i];
}
for (int i = 0; i < length; i++){
count[array[i] - min]++;
}
for (int j = 0;j < max - min + 1;j++)
{
while(count[j] --)
array[sum++] = min + j;
}
}
int main() {
int array[] = {4,7,8,9,10,6};
countingSort(array, 6);
printArray(array,6);
return 0;
}
I improve it in some places but not it gives me a output 6,7,8,9,10,6...
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;
}