1234
1234
Last edited by davesmaith; 02-27-2008 at 11:14 AM.
Hello and welcome to the C programming forums. Hopefully staying here a bit will help a lot with your understanding of the language.
To declare an array, requires knowledge of two things the array type, and size.
Let's say we need to store 10 numbers.
This allocates room for ten signed numbers. The type is int which on most modern machines is either 32, or 64 bits wide and the length is 10.Code:int number[10];
Reading in input from the user is a bit more tricky. Most will tell you to use buffers and parse the numbers and various other advice to handle improper input. For demonstrations purposes i will use a plain old scanf.
Asuming the input is correct, it will populate the array with values.Code:int i; for(i=0; i<10; i++) { scanf("%d", &number[i]); }
To search for the highest and lowest values, is simply a matter of logic rather than array features.
The logic is to maintain two extra variables that correspond to the maximum and the minimum. Then initialize those variables to the extreme opposites, meaning the maximum to the minimum possble number and minimum to the maximum possible number that an int can hold. (These values are defined as constants in the <limits.h> header file, and with intuitive names INT_MIN and INT_MAX.)
Then for every element of 'number' compare it to the current minimum and maximum, and should the element be less than the current minimum that element is the current minimum, and use the same logic to update the current maximum.
After all elements of the array have been accessed, the minimum and the maximum have been found.
I would give real source code, but your post appears to be a masked homework post. Surely, if you are a trainee teacher you have read code that deals with trivial tasks.
A typical example of ...cheap programming practices.Code:... goto johny_walker_red_label; johny_walker_blue_label: exit(-149$); johny_walker_red_label : exit( -22$);
dont worry i plan to be around for a while , its annoying when the students have a better understandin of c than you do, they learn through lego technics.
Code:# include <stdio.h> int main() { int i, number[i]; for(i=0; i<10; i++) printf ("enter a number"); scanf("%d",&number[i]); printf(" array 1 the number is %d",number[i]) ; system("pause"); return 0; }
>i am a traniee teacher
What are you teaching? If it's C or programming in general then you've got to do some learning before you're qualified to be a teacher. I would expect any beginner to C to be able to solve this problem with ease after maybe two weeks of study.
>And as for that matter, do you use C99 or C89?
C89. How do I know? C99 doesn't allow implicit function declarations anymore, so failure to include stdlib.h or manually declare the system function would throw an error on a conforming C99 compiler.
My best code is written with the delete key.
I was reluctant whether i could teach. I am not doing it as a profession, but rather part time. This site was a great help, in the way that it taught me where to expect the question, and to remember my own questions when i was learning. Oh and also how to formulate the answer so that it might be clean and correct, at least most of the times.
Dave: You need serious reading. You can not expect to win the respect of students if they outsmart you. Also, i would consider it very important to know what i am talking about, lest not talk at all. Half-spoken words are worse than not spoken at all.
The code you present shows that you need to start from scratch.
You can not form variable declarations ( there is not a correct array declaration in your code), you don't understand well what is a statement.
Your for loop should operate on multiple statements, but you haven't grouped them between {}, so that it has a chance to do so.
If as you can say, you can read C code, then do so. And try to be as simple as possible for the moment. Keep your statements clean, indent, think before you write.
Ask when something is confusing you.
Remember in programming the most important thing above all, is to understand how the flow of the program goes, being able to form it in your mind for short code parts will reassure the validity of the solution without having to revert to program proof theories. Language syntax comes at the next phase.
A typical example of ...cheap programming practices.Code:... goto johny_walker_red_label; johny_walker_blue_label: exit(-149$); johny_walker_red_label : exit( -22$);
You can in C99, but I'm not sure if that's what you're hinting at.
Probably the biggest error in the code is that the array is initialized with an uninitialized variable i. Will probably cause the program to crash as soon as it starts. This is assuming it would be compiled under C99, otherwise it wouldn't compile at all.
1234
Last edited by davesmaith; 02-27-2008 at 11:14 AM.
What is with this? Are you crazy? What does that even mean?
You shouldn't deface your replies like that you know, because now we have to extrapolate what the thread is about from the quality of people's replies, instead of you, the OP. That's a strategy that's doomed to fail because I can't rely on everybody to come through. Use some sense next time and don't ruin your posts after a few hours.
So in other words, as Dave is a first-year student planning to teach technology (food technology?) ... it was homework.
/1234