C++ average of negative elements in array [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 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;
}

Related

I dont know what i am doing wrong in the FindLargest Function [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 2 years ago.
Improve this question
I am new to programming.
I am solving a problem and getting very absurd values.
Any Fix ?
Program Req Output :
Write a function maxColumn which takes a matrix of any size. It should
find the largest element in each column of the array. You have to
store it in a single dimensional array and print the values on screen.
For example, if the matrix has following elements is 1 4 8 9 1 6 5 7 2
Your result array should contain following elements.Result[3] = {9,
7, 8} Note: Here we are calling 2D array as matrix
My Code :
#include <iostream>
using namespace std;
// Global Variables
const int rows = 100;
const int col = 100;
void findLargest(int arr[rows][col], int size)
{
int Max[] = {0};
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
Max[i] = arr[i][j];
if (arr[i][j] > Max[i])
{
Max[i] = arr[i][j];
}
}
}
for (int i = 0; i < size; i++)
{
cout << Max[i] << " ";
}
}
int main()
{
int arr[100][100] = {0};
int size = 0;
cout << "Enter the size = ";
cin >> size;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
cin >> arr[i][j];
}
}
cout << "Array input by User";
cout << endl;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << "Max of Each Column is = ";
findLargest(arr, size);
}
My Output : Enter the size = 3 1 2 3 4 5 6 9 8 7 Array input by User =
1 2 3 4 5 6 9 8 7 Max of Each Column is = 3 1 10
int Max[] = {0}; should have a size, otherwise, you'll be printing garbage values.
Also, in your inner loop:
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
Max[i] = arr[i][j]; // <-------
if (arr[i][j] > Max[i])
{
Max[i] = arr[i][j];
}
}
}
You're setting the maximum each time you scan a new element, and not when you see a bigger one.
Just Figured it out.
Just Write any constant Value in
int Max[100] = {0};

Represent spiral of numbers in a matrix [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 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

How to display numbers by line depending on the number in 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 6 years ago.
Improve this question
Hi I am new to C++ and I am having problem with displaying the numbers from 1,2,3, and so on depending on the user input.
For example, if input is 3, first line of output should be 1, next line should be 2 3, and the last line should be 4 5 6. Please see below screenshot:
The number of elements to display in the leftmost n columns is exactly:
So, the numbers in the first row form the A000124 integer sequence.
Therefore, you can just add the row index to respective value of the sequence, and print it only when row index is not greater than column index.
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
if(i <= j) {
cout << j*(j+1)/2+1 + i;
}
cout << '\t';
}
cout << endl;
}
return 0;
}
See the code live.
I hope this helps.
#include<iostream>
using namespace std;
int main()
{
int n, l=1;
cin >> n;
for(int j = 1; j <= n; j++) {
int k = l;
for(int i = 1; i <= n; i++) {
if(i < j) {
cout << " \t";
continue;
}
cout << k << "\t";
k += i;
}
cout << endl;
l = l + j + 1;
}
return 1;
}

C++ code not running as it should be [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
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.

Process terminated with status -1073741510? [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 9 years ago.
Improve this question
I have the following code that compiles without any errors but does not run. Can someone tell me the problem ?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int kenken[4][4];
kenken[2][2] = 3;
for(int i = 0; i < 5; i++)
for(int j = 0; j < 5; j++)
for(int k = 0; k < 5; k++){
if(i + j + k == 9){
kenken[0][0] = i;
kenken[0][1] = j;
kenken[0][2] = k;
}
if(i * j * k == 6){
kenken[1][0] = i;
kenken[2][0] = j;
kenken[3][0] = k;
}
if(abs(i - j)== 3){
kenken[1][1] = i;
kenken[1][2] = j;
}
if(abs(i-j) == 2){
kenken[3][1] = i;
kenken[3][2] = j;
}
if(i/j == 2){
kenken[0][3] = i;
kenken[1][3] = j;
}
if(i * j * k == 12){
kenken[2][2] = i;
kenken[2][3] = j;
kenken[3][3] = k;
}
cout << kenken[0][0] << " " << kenken[1][0]
<< kenken[2][0] << " " << kenken[3][0]
<< "\n\n";
}
return 0;
}
When j==0 you're going to get a division by zero error here:
if(i/j == 2){
As Ben suggested, a fix would be:
if( i == 2*j ) {