You are viewing our Forum Archives. To view or take place in current topics click here.
Learning C, need some help
Posted:
Learning C, need some helpPosted:
Status: Offline
Joined: Feb 26, 201212Year Member
Posts: 365
Reputation Power: 15
Status: Offline
Joined: Feb 26, 201212Year Member
Posts: 365
Reputation Power: 15
I am learning to write C by myself and learning about pointers, what I am trying to do is modify the following program from a previous example so that the max_min function uses a pointer instead of an integer to keep track of the current position in the array. The function declaration should not change
Any help would be much appreciated
Last edited by FR0NTLinealpaca ; edited 1 time in total
#include <stdio.h>
#define N 10
void max_min(int a[], int n, int *max, int *min);
int main(void)
{
int b[N], i, big, small;
printf("Enter %d numbers: ", N);
for (i = 0; i < N; i++)
scanf("%d", &b[i]);
max_min(b, N, &big, &small);
printf("Largest: %d\n", big);
printf("Smallest: %d\n", small);
return 0;
}
void max_min(int a[], int n, int *max, int *min)
{
int i;
*max = *min = a[0];
for (i = 1; i < n; i++) {
if (a[i] > *max)
*max = a[i];
else if (a[i] < *min)
*min = a[i];
}
}
---
Any help would be much appreciated
Last edited by FR0NTLinealpaca ; edited 1 time in total
#2. Posted:
Status: Offline
Joined: Jan 28, 201212Year Member
Posts: 324
Reputation Power: 12
Status: Offline
Joined: Jan 28, 201212Year Member
Posts: 324
Reputation Power: 12
I don't have time to go into this in detail so i'll take another look tomorrow but I can give you some small pointers.
Your missing {} brackets for your if statement in the main and the for loop in your function. While I'm not sure it will make a difference the C compiler runs top down so it is better to have it compile your functions before it reaches the main.
You also need to tab your code better to make it easier to read and check for errors. Is this the first programming language you've tried to learn or have you coded in something else before?
Your missing {} brackets for your if statement in the main and the for loop in your function. While I'm not sure it will make a difference the C compiler runs top down so it is better to have it compile your functions before it reaches the main.
You also need to tab your code better to make it easier to read and check for errors. Is this the first programming language you've tried to learn or have you coded in something else before?
- 0useful
- 0not useful
#3. Posted:
Status: Offline
Joined: Feb 26, 201212Year Member
Posts: 365
Reputation Power: 15
Status: Offline
Joined: Feb 26, 201212Year Member
Posts: 365
Reputation Power: 15
I am also learning Java but was told that learning C isn't that bad since some of it is similar so I thought i would try but I am finding it a pain in my ass.
I will edit my code to make it more readable.
I will edit my code to make it more readable.
- 0useful
- 0not useful
#4. Posted:
Status: Offline
Joined: Jan 28, 201212Year Member
Posts: 324
Reputation Power: 12
Status: Offline
Joined: Jan 28, 201212Year Member
Posts: 324
Reputation Power: 12
If you have not got all the basics from Java I would stick to that for now. C requires a lot more memory management which gets taken care for you in Java, once you have the basics down and you are comfortable with them I would move onto C as it is a more powerful language.
I also find C a pain but once you understand it, it gets a lot easier.
I also find C a pain but once you understand it, it gets a lot easier.
- 0useful
- 0not useful
#5. Posted:
Status: Offline
Joined: Jun 29, 201311Year Member
Posts: 211
Reputation Power: 13
Status: Offline
Joined: Jun 29, 201311Year Member
Posts: 211
Reputation Power: 13
-Pig wrote I don't have time to go into this in detail so i'll take another look tomorrow but I can give you some small pointers.
Your missing {} brackets for your if statement in the main and the for loop in your function. While I'm not sure it will make a difference the C compiler runs top down so it is better to have it compile your functions before it reaches the main.
You also need to tab your code better to make it easier to read and check for errors. Is this the first programming language you've tried to learn or have you coded in something else before?
1) You don't need brackets for a lot of things, whitespace works too as long as it immediately follows. Good suggestion for OP though. @OP - Only ever not use brackets if the instruction after the control declaration is 1 line.
2) It's evident OP would know the compiler runs "top down" as he prototyped a function at the start.
3) 2 soft tabs are valid tabbing code style guidelines. I personally prefer an actual tab but whatever.
@OP - your idea is kinda pointless since, unless you create a thread with a loop that continually prints the index int used in your for loop, you'll never see it change and value, when printed after your function call, will always be == N.
Nonetheless (you have to change the function declaration):
#define N 10
void max_min(int a[], int n, int *max, int *min, int *pointlessTracker);
int main(void)
{
int b[N], i, big, small;
printf("Enter %d numbers: ", N);
for (i = 0; i < N; i++)
scanf("%d", &b[i]);
int index = 0;
max_min(b, N, &big, &small, &index);
printf("Your pointless index: %d \n", index); // will equal N
printf("Largest: %d\n", big);
printf("Smallest: %d\n", small);
return 0;
}
void max_min(int a[], int n, int *max, int *min, int *pointlessTracker)
{
*max = *min = a[0];
for (*pointlessTracker = 1; *pointlessTracker < n; ++*pointlessTracker) {
if (a[*pointlessTracker] > *max)
*max = a[*pointlessTracker];
else if (a[*pointlessTracker] < *min)
*min = a[*pointlessTracker];
}
}
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.