Results 1 to 9 of 9

C++ problem

This is a discussion on C++ problem within the Off Topic forums, part of the Entertainment category; Ok, this is not a syntax problem, but rather a structural problem. I have this array (with values put in ...
Page: 1


  1. #1
    Judas Iscariot
    Guest

    Default

    Ok, this is not a syntax problem, but rather a structural problem.

    I have this array (with values put in by the user) of binary numbers and I need to change the entire thing into a normal number.

    Here's the function I made to change the values after the user inputs them.... syntax is correct, however, I think the result given might not be the thing I want. I'm not even sure what it gives :huh:



    int conversion (int [])
    {
    int T; //declaration of T so I can bypass some error...
    for (int i=0; i<=40; i++) //moving within each value of the array
    {
    if (int = o) //binaries... 1 or 0
    M = 0;
    else
    int M = 1*pow(2,i) + 1*pow(2, i+1); //1 times 2 to the power i = conversion from binary to normal value. sum is for adding the next part

    for (int u=0; u<=39; u++) //parallel computations, the first part so I can make a copy of M unchanged( variable n), and then change M
    // and add it to it&#39;s original value M. notice the difference in the counters
    {
    int N = 1*pow(2,i) + 1*pow(2, i+1); //i is from the previous counter

    int C = M+N;
    int T=C+N;
    } //end 1st for

    } //end 2nd for
    cout<< T;
    } //end user defined function



    Halp guise please!

  2. #2
    Senior Member festo's Avatar
    Join Date
    Mar 2006
    Location
    England
    Posts
    2,820

    Default

    You sure its not syntax problem? For instance;

    if (int = o) should be if(int == o),
    Where have u declared M?
    int conversion (int []) looks like an array operator from C#.....never seen it used in C/C++
    int M = 1*pow(2,i) + 1*pow(2, i+1); //1 times 2 to the power i = conversion from binary to normal value. <-- Why the *1

    Biggest suggestion i can give you is start again

  3. #3
    Judas Iscariot
    Guest

    Default

    I made a few mods to the function that I just posted while I was posting it. The exe ran without the changes I had made here :P
    int[] <---- this is an array yes, and it&#39;s declared in main. Put it as int[] so you&#39;d recognize it as an array.
    int conversion is my function, and it takes int[] as a parameter.

    int M = 1*power(2,i).... it was one because I figured the 1 would keep calculations clean and simple, whilst raising to the exponent (the counter). Now I changed the 1 for a pointer y of the array int[]


    I made a few other modificiations it runs, however, i&#39;m not sure it gives me what I want yet

    int conversion (int [])
    { int T;
    int*y=new int[40];
    for (int i=0; i<=39; i++) //moving within the array
    {
    int M = 1*pow(2,i) + y[i]*pow(2, i+1);
    if (y == 0) int M = 0;
    for ( int u=0; u<=39; u++)

    {
    int N = y[u]*pow(2,i) + y[i]*pow(2, i+1);
    int C = M+N;
    int T=C+N;}

    }
    cout<< T;
    }

  4. #4
    Senior Member festo's Avatar
    Join Date
    Mar 2006
    Location
    England
    Posts
    2,820

    Default

    no idea how that compiles

    the array your passing in....where is it used?
    Why are you defining M and T twice?
    Memory leaks? = int*y=new int[40]; - never gets deleted
    if (y == 0) int M = 0; - y is a pointer to the array of ints therefore y will never be 0?

  5. #5
    Judas Iscariot
    Guest

    Default



    lol I dont have ksc though XD



    no idea how that compiles

    the array your passing in....where is it used?
    Why are you defining M and T twice?
    Memory leaks? = int*y=new int[40]; - never gets deleted
    if (y == 0) int M = 0; - y is a pointer to the array of ints therefore y will never be 0?[/b]
    the array i&#39;m passing is used in the computations. Well I WANT to use that one
    huh?
    Yes, it has a memory leak. However i&#39;m not in Data structures yet, so i&#39;m not really worried about that just yet :unsure:
    Y CAN be zero, because the value of the array is either 1 or 0 depending on where the counter is. The array y[i] moves down as i++, each time it increases the array takes on the value that was inputed by the user. Or so i think

  6. #6
    Senior Member festo's Avatar
    Join Date
    Mar 2006
    Location
    England
    Posts
    2,820

    Default

    y cannot be zero, y is a pointer to memory - memory that you have already allocated
    Can you point out where in the code you are using where the array is passed in

  7. #7
    Judas Iscariot
    Guest

    Default

    I changed it... now the function makes the array and also takes the user input....
    Well that problem with the pointer is bad. Should it be &y instead of y? I need the value of the array, not the memory!
    I&#39;m not giving up just yet!

    int conversion ()
    //previously int conversion (int [])
    { int T = 0;
    int*y=new int[40];
    for( int z=0;z<=40-1;z++)
    {
    cout<<"Entrar valor"<<endl;
    cout<<"este es valor numero ="<<z<<endl;
    cin>>y[z];

    for (int i=0; i<=39; i++) //moving within the array
    {
    int M = 1*pow(2,i) + y[i]*pow(2, i+1);
    ... rest is unchanged

    OH yes, TYVM for your help so far

  8. #8
    Senior Member festo's Avatar
    Join Date
    Mar 2006
    Location
    England
    Posts
    2,820

    Default

    theres a loop inside a loop there that probably shouldnt be

    Travelling home now so ill have a look properly when i get home

  9. #9
    Redemption
    Guest

    Default

    Any link/website you guys can share that teaches C++?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •