Thread: what is wrong with my program?

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Code:
    for(int y=0 ; y<b+1; y++)
    	s1[y]=*(function(4,n,b,s)+y);
    How is s1 defined?

    What are you trying to do with:
    Code:
    function(4,n,b,s)+y
    Why are you adding y to the function return value?

    Jim

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    thank you both but I have like 5 or 6 arrays in my program and I have wrote the whole program with arrays. if I wanna change them to vectors then I'll have to develop the program from the beginning. I've got a question here. if I use vector in my function then I should return vector<int>. and I have to assign it to an array later, when the function is called. can I do such a thing?
    I feel bad no one is paying attention to you.

    You can use vectors and arrays interchangeably, sure. The copy algorithm can help you move things around.
    Code:
    int s[1000];
    size_t size;
    std::copy(thevector.begin(), thevector.end(), s);
    size = thevector.size();
    thevector.clear();
    // use s
    But let's talk about why your program doesn't work.

    Code:
    int* function1(int a,int c, int b)
    {
    int s[1000];
    	.
    	.
    	.
    	.
    	return s;
    
    }
    Returning arrays from functions is funny business. Unless they are allocated on the free store or in static storage (declared with the static keyword) you cannot return an array. You return a dangling pointer (a pointer that points to released memory).
    Code:
    int* function(int a, int c, int b) 
    {
       static int s[1000];
    
       return s;
    }
    Now, the static keyword is the swiss army knife of C and C++. In this context, a static variable will persist for the duration of the program, even though it is only accessible in function -- even after the function returns, s is still alive, so you can return pointers to it and therefore extend its scope. One problem with this approach is that no matter how often function is called, it will operate on the same array. If you need it to work with several different arrays, then allocate the pointer on the free store. static does other things in other different contexts, and this may not be what you want after all.

    Another way to return arrays is to just return the array a function was passed as an argument.

    Code:
    int* function (int*base, int a, int c, int b); // return base
    This is the best approach, because it leaves the caller with the responsibility of allocating and releasing base's memory. This is a lot more flexible because you can store base however you like, and function can still do what it needs to do.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jimblumberg View Post
    [
    What are you trying to do with:
    Code:
    function(4,n,b,s)+y
    Why are you adding y to the function return value?
    Pointer arithmetic.
    *(p + n) = p[n]

    Quote Originally Posted by whiteflags View Post
    I feel bad no one is paying attention to you.
    I feel bad I missed that reply -_-
    I blame it on not getting an email notification.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    I'd been using arrays for years too and together with assembly I consider it a good experience. With a low level programming knowledge I could easily understand all the benefits of containers, every new thing was coming with "oh yes, finally I get what I missed so much".

    If I had learnt vectors first, I wouldn't have bothered with arrays at all probably, and I wouldn't have gained this knowledge and experience.

    There are many here who would disagree with you, and I am one of them.
    Learn the language properly, then move into the advanced parts (ie C arrays and C api).
    I know there are. C++ is a language which allows you to mix low level concepts with high level and modern techniques, so I wouldn't be so sure if starting with the second one is always a "proper way".

    It's something similar to learning Pascal (or C++ alternatively) after PHP - "why do we need these types at all?"

    Well, at least there is one thing I know - no matter what way you choose, you have to gain experience.

    And if you damn all the low level concepts so much, why won't you just move to C# or Java?
    I don't say I use them, I know how they can affect my design badly. But because of the experience gained with them, I know when and how to use them.
    Last edited by kmdv; 02-28-2011 at 01:34 PM.

  5. #20
    Registered User
    Join Date
    Feb 2011
    Posts
    35
    my whole function is this: (it is supposed to divide a by c and puts the fractional parts-up to b digits- in the following elements of array s)
    Code:
    int* function(int a,int c, int b, int[])
    {
    	int i=0;
    	int q;
    	while(i<=b+2)
    	{
    		q=a/c;
    		s[i]=q;
    		i++;
    		if (i==1)
    			{
    				s[i]=-1;  //determines the place of "."
    				i++;
    			}
    		a=(a-c*q)*10;
    	}
    	
    	return s;
    
    }
    it returns s which is a pointer to the first element of array s.
    by writing this:
    int s1[1000];
    for(int y=0 ; y<b+1; y++)
    s1[y]=*(function(4,n,b,s)+y);
    I meant the values of the array s(which are now changed) to be placed in array s1.
    since function(4,n,b,s) returns a pointer, I use ] a * operator to refer to it's value. and I'm adding y to the returned pointer so that it goes to the next spaces of memory(array spaces are in a sequence) in each iteration.(should I write 4*y or y?)...I thought the values of the elements of the array could be assigned to array s1 this way.

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by kmdv View Post
    I know there are. C++ is a language which allows you to mix low level concepts with high level and modern techniques, so I wouldn't be so sure if starting with the second one is always a "proper way".
    It most certainly is, since it saves time, frustration and bugs, and it is what makes C++ C++. Otherwise you can just start off with C. What's the point? Low-level stuff is only necessary for the advanced stuff in C++ due to abstraction and encapsulation.

    And if you damn all the low level concepts so much, why won't you just move to C# or Java?
    I don't say I use them, I know how they can affect my design badly. But because of the gained experience with them, I know when and how to use them.
    I don't damn them. I just damn the way of teaching newbies those techniques before they're ready.
    I've used a lot of low-level techniques to implement things such as smart pointers and containers.
    But I'm not a newbie.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User
    Join Date
    Feb 2011
    Posts
    35
    I thank you all, esp whiteflags!
    thank to you, I think I'm gettin a deeper undrestanding of arrays, static arrays and stuff.

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Farnaz View Post
    thank to you, I think I'm gettin a deeper undrestanding of arrays, static arrays and stuff...instead of learning proper modern C++ such as std::array, std::vector, et all! Thank you so much!
    Fixed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Feb 2011
    Posts
    35
    I know about vectors and I do use them in my programs but I just thought it would be more convenient to use arrays in this case.

  10. #25
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    int s1[1000];
    for(int y=0 ; y<b+1; y++)
       s1[y]=*(function(4,n,b,s)+y);
    I meant the values of the array s(which are now changed) to be placed in array s1.
    Yes, that will copy over the array just fine. But you are also calling the function b + 1 times and changing s b + 1 times. Even if function works when you tested it, did you test using it this way? Calling function so many times messes up function's math, because each time, s is different. Call function exactly once, and then copy over s to s1.

  11. #26
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Farnaz View Post
    I know about vectors and I do use them in my programs but I just thought it would be more convenient to use arrays in this case.
    @Elysia:
    So, he probably knew about vectors yet before arrays. And this is the result I mentioned. If he doesn't learn about arrays and (try to) use them now, he will not know how bad they are.

    To be clear: I am not trying to convince you that starting with low level things is just better. I want to show advantages of starting with low level stuff.

    EDITED

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Farnaz View Post
    I know about vectors and I do use them in my programs but I just thought it would be more convenient to use arrays in this case.
    Using arrays over vectors is never convenient. If it is, you are doing something very wrong.

    Quote Originally Posted by kmdv View Post
    To be clear: I am not trying to convince you that starting with low level things is just better. I want to say, that learning started with low level stuff has its advantages.
    Yes. Complicated programs, lots of bugs and frustration.
    It's akin to learning something by hand in a complicated way. Then, a much more easier and better way is revealed and you will cry out "why didn't we do this in the first place?!".

    Sigh. Ugh. Whatever. Frustration creeps over me.
    Forget this discussion now. I know I intend to.

    Btw, gender assuming is bad. Remember that lesson!
    Last edited by Elysia; 02-28-2011 at 02:01 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    Join Date
    Feb 2011
    Posts
    35
    Quote Originally Posted by kmdv View Post
    @Elysia:
    So, he probably knew about vectors yet before arrays. And this is the result I mentioned. If he doesn't learn about arrays and (try to) use them now, he will not know how bad they are.
    EDITED
    It's "She" actually!

  14. #29
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Elysia View Post
    Yes. Complicated programs, lots of bugs and frustration.
    It's akin to learning something by hand in a complicated way.
    No need to write huge, obfuscated and complicated programs with tons of bugs. I think he made a good step to try out regular arrays and feel their weaknesses on his own.

    Quote Originally Posted by Elysia View Post
    Then, a much more easier and better way is revealed and you will cry out "why didn't we do this in the first place?!".
    As I already said, the benefit of this is the expierience, which you might not feel like to get later.

    Sorry if I'm starting to offtop.

    Quote Originally Posted by Farnaz View Post
    It's "She" actually!
    AFAIK it's HE. Is he?

  15. #30
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Not to stoke the fire, but I think my earlier post got lost in the shuffle so Farnaz be sure you read everything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is wrong with this program??
    By cprog12 in forum C Programming
    Replies: 3
    Last Post: 11-24-2010, 12:48 PM
  2. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  3. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  4. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM

Tags for this Thread