Hi guys, I really need your help with these assignments since I can't figure them out.
http://b.imagehost.org/0320/Capture1.jpg
Sorry if I made translation mistakes.
Hi guys, I really need your help with these assignments since I can't figure them out.
http://b.imagehost.org/0320/Capture1.jpg
Sorry if I made translation mistakes.
1) Read the homework policy on this site
2) If you do need help with homework you have to provide your best effort (preferably in code) as well as the impression that you understand the problem domain in general as well as where you are stumped in your code.
Most people here have already been through school and feel no need to do somebody else's homework. They will help along though if you prove that you are taking it seriously and are responsible about getting it done.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
Please not that this forum (and every other forum I've seen) had a strict policy of not doing homework for people. It doesn't help you learn anything anyway.
We will, however, help you out with specific aspects of problems. What exactly are you having problems with? Do you understand the questions? What work have you done so far?
The more specific you are (and the more effort to tackle the problems yourself you show) the more likely you are to get helpful answers.
And he is gone.... So depressing that people just expect other people to do everything for them nowadays.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
Yeah you're both right. Then maybe you could tell me whats wrong with this program
Code:#include <stdio.h> int main() { int array[100], n, i, much; printf("enter length of array \n"); scanf("%d", &n); for (i=1;i<=n;i++) { printf("enter a number for %d \n", i); scanf("%d", &array[i]); } for(i=1;i<=n;i++) { if ((array[i] > 0) && (array[i+1] > 0)) {much++;} } printf("%d", much); return 0; }
thats the first assignment.
Arrays are indexed starting from zero. So in your program, since your loop goes from 1 to n inclusive, you need to index it like this: array[i - 1].
Better to loop like this: for (i = 0; i < n; i++) and use array[i]. That's more common.
much needs to be initialized to zero. You can do that at the place you define it.
In the second loop you are indexing too far.
I believe that the first assignment ask for a dynamically allocated array, the length of which depends on input for n. You are assuming that there will be no more then 100 integers. If you haven't learned about dynamically allocated memory yet, you can ignore this remark.
Other then that:
(1) Indexes for an array of N elements run from 0 to N-1, inclusive.
(2) much is not initilized before first use, this will produce undefined results.
(3) You should be careful with expressions like array[i+1], you will go out of bounds eventually.
EDIT: Bah. Beaten.
Thanks with this that's one down