7 Replies - 628 Views - Last Post: 13 March 2011 - 06:21 AM Rate Topic: -----

#1 h131200  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-March 11

how to create an array and send it to another form like a variable

Posted 12 March 2011 - 04:35 AM

Hey .
I have projekt work " Advanced calculator " that the user can calculate derivative .
I've been thinking about an array like this .
2 forms
the first one contains textbox and button .
the other on label .
the user have to specify the number of how many products in the function by typing in the textbox in the first form . then the button send this number to the other form .
now when the secund form recesivs this numer , it will creat an array of length [ number from the fist form ]

and then run foreach kod .
f(x) = X^n . will be n*X^n-1 .
så the number will determine how many X^n . so foreach( ( X^n ) in array ) derivative is n*X^n-1 .


I do not know if im thinking right.

when i use this in console application
int  [] r = new int [10] ;
 for ( i= 0 ; i < r.Length; i=i+1)
{   
     console.writeline (r[i]) ;
}


i get output 1, 2, 3 ..... 9

but when i use it in Form applicatoin i got the sum of the number .

This post has been edited by JackOfAllTrades: 12 March 2011 - 05:42 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: how to create an array and send it to another form like a variable

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: how to create an array and send it to another form like a variable

Posted 12 March 2011 - 04:56 AM

That looks like Java to me, this is the C/C++ forum.
Was This Post Helpful? 0
  • +
  • -

#3 ryuurei  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 09-March 11

Re: how to create an array and send it to another form like a variable

Posted 12 March 2011 - 05:07 AM

View PostAphex19, on 12 March 2011 - 04:56 AM, said:

That looks like Java to me, this is the C/C++ forum.


Are you sure? I'm not entirely familiar with either but that looks like C# to me. I recall seeing the Console object referenced in C# code before.
Was This Post Helpful? 0
  • +
  • -

#4 h131200  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-March 11

Re: how to create an array and send it to another form like a variable

Posted 12 March 2011 - 05:25 AM

:s yes , this is C#

:s yes , this is C#
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6074
  • View blog
  • Posts: 23,540
  • Joined: 23-August 08

Re: how to create an array and send it to another form like a variable

Posted 12 March 2011 - 05:35 AM

Moved to C#
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6074
  • View blog
  • Posts: 23,540
  • Joined: 23-August 08

Re: how to create an array and send it to another form like a variable

Posted 12 March 2011 - 05:43 AM

I think you're going to have to show more code here. Post your code IN CODE TAGS as shown below:

:code:
Was This Post Helpful? 0
  • +
  • -

#7 h131200  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-March 11

Re: how to create an array and send it to another form like a variable

Posted 13 March 2011 - 05:39 AM

View PostJackOfAllTrades, on 12 March 2011 - 05:43 AM, said:

I think you're going to have to show more code here. Post your code IN CODE TAGS as shown below:

:code:




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace test_send_data
{

// imoprt data from another form 

    public partial class MySecondForm : Form
    {
        public int Myint;
        public MySecondForm()
        {
            InitializeComponent();
        }

        public MySecondForm(int MyVal)
        {
            InitializeComponent();
            Myint = MyVal;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            lblshowresult.Text = Myint.ToString();

        }

        private void MySecondForm_Load(object sender, EventArgs e)
        {
            string s = "";
           
            for (int i = 0; i < (Myint); i++)
            {
                s += "kX^n+";

                lblshowresult.Text = "f(x) =" + s;

            }
        }     
            }

             // derivative when butten click 

        private void button1_Click_1(object sender, EventArgs e)
        {
                // creat array of length myint which i import from rhe first form 
            double[] k = new double[Myint+1];
            double[] n = new double[Myint+1];
             //  array items 
            n[0] = Convert.ToDouble(txtbxn1.Text);
            n[1] = Convert.ToDouble(txtbxn2.Text);
            k[0] = Convert.ToDouble(txtbxk1.Text);
            k[1] = Convert.ToDouble(txtbxk2.Text);

       
            double f ;
            double c; 
         
            string s = "";
            string s2 = "";

            foreach (double i in n )
            {
                f = (i - 1);
                
                
              foreach (double r in k)
            {
                c = (r * i);
                s += Convert.ToString(c) + "X^" + Convert.ToString(f) + "+";
            }
              txtbxresul.Text = "Derivative is  :\r\n f(x) =" + s;
            }  
                
            }
                

        }




my problem now is
http://img718.images...capturealh.jpg/

the derivative should be f'(x) = 12X^3 + 12X^3 .
why does the program repeat the items many times ???

This post has been edited by h131200: 13 March 2011 - 05:41 AM

Was This Post Helpful? 0
  • +
  • -

#8 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: how to create an array and send it to another form like a variable

Posted 13 March 2011 - 06:21 AM

In line 53 and 54 you declare arrays of double that are Myint+1 in size (and your comment above them is wrong). These arrays are initialized to the default value of a double, which is zero. You then put values into the first two array spots.

Line 68 you iterate through all the doubles in the array, even those that have default values. Same with line 73. This is where all the odd -1X stuff comes from.

As for the repeating, you have 4 values that you have split into 2 groups, and you match each group to the other group. That makes 2 times 2 = 4 values you are going to output. You need to rethink your logic here.

This post has been edited by Momerath: 13 March 2011 - 06:22 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1