Question:
Can Someone help explain to me this C Programming Point stuff?
?
2010-02-03 14:14:59 UTC
I have ten problems I must do for my homework that involve writing programs using pointers in C.
Now I'm new to programming so its very confusing for me.
This is the easiest problem on the handout yet I still do not know how to do it.
The teacher didnt even give us examples to follow or anything so that is why I am asking for a fully detailed explanation for help with this problem.
(I was sick and could not go to tutoring today :[ )
Here it is:
Write a program using pointers to implement a solution to the following problem:
a) Create a memory block to store 30 doubles (use the modified "Memory allocation for n integers" idiom)
b)Read in up to 30 doubles from the standard input; stop reading when any of the following conditions are true (using the "Block traversal with a sentinel" idiom)
(1.) 30 doubles have been read
(2.) End of file has been reached
(3.) A negative value has been encountered

c) find the maximum double value stored in the block

Please help me learn =(
Five answers:
Mantis
2010-02-03 14:49:51 UTC
It sounds like you're working on arrays. This is a very fundamental part of the language that you'll use a lot from here on. Here's the quick rundown. You know how to create a variable, right?



int temperature; //allocates one number.



So what happens if you need to record more than that? What if you want to review the temperatures for a whole week? A whole year? Or, in the case above, you want to record 30 doubles from the user. You could do this:



int number1;

int number2;

int number3;

...

int number30;



But this is not a clean solution. If you wanted to print these all out, you'd need 30 print statements. So we get arrays.



int number[30]; //allocate space for 30 numbers.



You can refer to this as number[0] for the first one, number[1] for the second, and so on up to number[29]. (Don't try to do number[30]... it is a programming error to do so). Better, you can do this in a loop. Take a look at this framework, and I think you'll see where you can use it in the assignment.



const int maxValues = 30;

double readValue[maxValues];



for (int i = 0; i < maxValues; i++)

{

// Whatever happens in here will happen 30 times. If you read,

// like cin << readValues[i] it'll read into element number i of your array.



// If you need to exit the loop early, for example your reach the end, use this command:

break; //exit loop early.

}



Hopefully this gets you started. If arrays aren't the sticking point, let us know what is.
jplatt39
2010-02-03 15:58:39 UTC
I don't know what the "modified "memory allocation for n integers" are but...



A pointer is a variable (usually) which points to a specific address or group of addresses in memory. You assign in space in one of two ways. One is very straightforward. For int a and pointer to int *Ptr



Ptr=&a;



or the above says assign to Ptr the address of a -- & is the address operator. The other would be Ptr=(int *) malloc(sizeof(int)); That means allocate an address the size of an int and cast the pointer to it as an int.



There are two points to emphasize before going further. 1. malloc returns a void pointer which you can cast as anything. Sometimes (depending on the compiler) the compiler will do it. Don't rely on it. Cast it. 2. I used sizeof(int). You can use sizeof(anything), where anything is floats, doubles, chars unsigneds, longs, or even predefined structs but to use this example on many systems the sizeof(int) returns 4. Don't use malloc(4) or any other number you know your compiler uses for the size of a type. Use the sizeof(type) function.



Now you may or may not know that what you can also do is Ptr=(int *)malloc(sizeof(int)*6); and then you have an array of six ints. In fact most C compilers pass large arrays as pointers to economize on the use of memory. Pointer decay is another topic. What is the difference, in a case like this, between an array you declare with int a[6] and a pointer? Simple: in the overhead which the compiler handles an array you declare as such has memory allocated for it and deallocated when the program ends. NO discussion of malloc() should not go into free() as in free(Ptr) rather than free(Beer) or free(Software). If YOU grab the memory with malloc YOU have to give it back. If you don't that memory will be unusable by the OS until it reboots or restarts after a shutdown. The compiler will not pick up after you.



With the tutorial I've linked to that information should be sufficient for you to better understand the problem.
?
2016-09-22 13:03:27 UTC
How do you anticipate a path to get replaced by means of a brief reply right here? Read your guide and ask extra distinct questions. There are a couple of dozens of dossier features break up in 2 modes, buffered and unbuffered. EDIT: If you may have ever used a pc you need to recognise what it's to avoid wasting a dossier. Those operations in C are supposed to furnish that capability, the probability of storing the software's output. Obviously it is usually valuable to make use of present records for enter as good, that is why there are dossier studying features.
JoelKatz
2010-02-03 14:22:25 UTC
What do you need help with? Do you understand the idioms involved? Do you know how to allocate memory and how to use it to store a number of variables of the same type?
?
2010-02-03 14:27:14 UTC
To be entirely honest, I'm not sure what you're asking. However, a good place to start would be:

http://www.cplusplus.com



Very good reference site. When I Googled "Memory allocation for n integers", I got this:

http://www.cplusplus.com/reference/clibrary/cstdlib/malloc/



I'm not gonna write code for you, but in pseudo code you should do something like:



START

create memory space for numbers

count = 0

highest = 0

new array value[30]



WHILE (count < 30) AND (NOT EndOfFile) AND (value >= 0)

value[count] = read from file



IF value[count] > highest THEN

highest = value[count]

ENDIF



WHILE END

END





I hope that gets the general point across...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...