I have just started learning how to program and I came across an assignment whereby I have to create an array wherein the weight of the elephant seals is read & printed. Can someone please tell me what's wrong with my code, because I can read in the data but my data is being printed wrong when I call the printArray function. Below is my code:
/*
compute the weight average of elephant seals using an array that will read in the data
*/
#include <stdio.h>
void arrayReadingData(const int sizeP) //array to read in the weight of the elephantseals
{
int weightP[sizeP];
for(int i = 1; i <= sizeP; i )
{
printf("Elephant seal %d weight is: ", i);
scanf("%d", &weightP[i]);
}
}
void printArray(const int sizeP)
{
int weightP[sizeP];
for(int i = 1; i <= sizeP; i )
{
printf("%d\t", weightP[i]);
}
}
int main(void)
{
int size;
printf("How many seals do you want to calculate the average weight for: ");
scanf("%d", &size);
arrayReadingData(size);
printArray(size);
};
CodePudding user response:
I redid the code, after learning from the comments and I came up with this solution, the problem is that now I can only read in 10 numbers and print 10 numbers if I read in 11 numbers and try to print it I get an error stating zsh: segmentation fault
/* compute the weight average of elephant seals using an array that will read in the data */
#include <stdio.h>
void arrayReadingData(const int sizeP, int weightP[]) //array to read in the weight of the elephant seals {
for(int i = 0; i < sizeP; i )
{
printf("Elephant seal %d weight is: ", i);
scanf("%d", &weightP[i]);
}
}
void printArray(const int sizeP, int weightP[]) {
for(int i = 0; i < sizeP; i )
{
printf("%d\n", weightP[i]);
}
}
int main(void) {
int size;
int weight[size];
printf("How many seals do you want to calculate theaverage weightfor:");
scanf("%d", &size);
arrayReadingData(size, weight);
printArray(size, weight);
}
CodePudding user response:
You are doing the for loop wrong, your loop should start at i = 0 to i < sizeP, when i = sizeP weights[sizeP] will be assigned an arbitrary value.
Update: I just noticed you are declaring the array as a local variable to each function and declaring it the wrong way, you should declare that array in the main function and pass it to your functions instead
Here is a solution you can use
/*
compute the weight average of elephant seals using an array that will read in the data
*/
#include <stdio.h>
#define SIZE 3
void arrayReadingData(int weightP[]) //array to read in the weight of the elephantseals
{
for(int i = 0; i < SIZE; i )
{
printf("Elephant seal %d weight is: ", i);
scanf("%d", &weightP[i]);
}
}
void printArray(int weightP[])
{
for(int i = 0; i < SIZE; i )
{
printf("%d\t", weightP[i]);
}
}
int main(void)
{
int weights[SIZE];
arrayReadingData(weights);
printArray(weights);
};
Note: if you want the size to be given by the user you can use pointers and allocate memory dynamically
Here is how you can achieve that:
/*
compute the weight average of elephant seals using an array that will read in the data
*/
#include <stdio.h>
#include <stdlib.h>
void arrayReadingData(int weightP[], int size) //array to read in the weight of the elephantseals
{
for(int i = 0; i < size; i )
{
printf("Elephant seal %d weight is: \n", i);
scanf("%d", &weightP[i]);
}
}
void printArray(int weightP[], int size)
{
for(int i = 0; i < size; i )
{
printf("%d\t", weightP[i]);
}
}
int main(void)
{
int* weightsArray;
int size;
printf("Enter the size of the array:\n");
scanf("%d", &size);
weightsArray = (int*)malloc(size*sizeof(int));
if(weightsArray == NULL){
printf("couldn't allocate memory");
exit(1);
}
arrayReadingData(weightsArray, size);
printArray(weightsArray, size);
free(weightsArray);
};