0

I want to write code for calculating some values and put these values in a 2-dimensional array. I have written very simple form of this problem.

This code gives me no output.

I want to have one for example m by n array or matrix with elements which are calculated by specific function.

#include "stdafx.h"
#include "iostream"
#include "cmath"
using namespace std;

double x;
int i,j;   
double A[10][10];
double M;

double function (double A,double x)
{
     for (i = 0; i = 9; ++i)
         for (j = 0; j = 9; ++j)
             M = 10 + x + A;
     return  M;
}

int _tmain(int argc, _TCHAR* argv[])
{
    double y;
    cin >> x;
    y = function(A[10][10], x);
    cout << y;

    return 0;
}
3
  • Perhaps the compiler objected to your indentation? Or your wanton mis-use of global variables. That aside, what output were you expecting? Commented Dec 23, 2013 at 19:55
  • 1
    "i < 10" and not "i = 9" Commented Dec 23, 2013 at 19:56
  • OP wants to know how to initialize a matrix in a function... Commented Dec 23, 2013 at 20:19

4 Answers 4

2

This code has no any sense

{ for (i=0;i=9;++i) for (j=0;j=9;++j) M=10+x+A; return  M; }

You are assigning 9 to i and j in the loops. As 9 is not equal to 0 then these conditions i = 9 and j = 9will be always converted to true and the loops will be infinite. Also it is not clear what you are trying to calculate by using one more invalid expression M=10+x+A

Sign up to request clarification or add additional context in comments.

2 Comments

I mean I and j=10 ,I want to calculate elements of matrix or array.each element of matrix (M[i][j]) can be calculated by specific function.
Name M is not defined as an array by you. So I still do not understand what you are trying to do.
1

You use assignment = instead of equality == in your loop condition. Since 9 happens to convert to true your loops are infinite loops (BTW, you probably want to use i != 10 and likewise for j anyway).

Comments

0

It looks like you intended to do:

 for (i = 0; i <= 9; ++i)
     for (j = 0; j <= 9; ++j)

You were missing the <. i < 10 would be the more standard usage.

Comments

0

Following may help you to understand how initialize array.

double createValueFromIndices(int x, int y)
{
    return 10. * x + y; // or any wanted value;
}

void initArray(double (&a)[10][10])
{
     for (int x = 0; x != 10; ++x) {
         for (int y = 0; y != 10; ++y) {
             a[x][y] = createValueFromIndices(x, y);
         }
     }
}


int main(int argc, char** argv)
{
    double a[10][10];

    initArray(a);

    return 0;
}

1 Comment

@user3120411: There is no output, but the array a will be initialized. So a[x][y] == 10. * x + y, for example a[4][2] == 42.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.