I have to write functions that manipulates an array containing numbers (doubles).
I have to use pointers for all arrays to do this.
One function prints the array, the next one triples each element in the array, and the last one reverses the array.
Heres my code so far-
Code:
#include <stdio.h>
#include <ctype.h>
void tripleEach(double * array , int arraySize)
{
int i;
for (i = 0; i < arraySize; i++)
{
*(array + i) = (*(array + i) * 3);
//printf("%lf ", *(array + i));
}
}
void reverse(double * array, int arraySize)
{
int i;
int j = 0;
for( i = arraySize/2; i >= 0; i--)
{
*(array + i) = *(array + j);
j++;
}
}
void printArray(double * array, int arraySize)
{
printf("\n");
int i;
double num;
for(i = 0; i<arraySize; i++)
{
printf("%lf ", *(array+i));
}
}
int main()
{
int i;
double * numArray;
int size;
double num;
printf("Please enter a number of floating numbers to be entered:\n");
scanf("%d", &size);
/*** Enter a line of code to allocate memory for the array here ***/
(double *)malloc(size * sizeof(double));
for (i=0; i<size; i++)
{
scanf("%lf", &num); //read in a double entered by user
*(numArray+i) = num;
}
printArray(numArray, size);
reverse(numArray, size);
printArray(numArray, size);
tripleEach(numArray, size);
printArray(numArray, size);
return 0;
}
My program crashes after I type in the numbers that will go in the array. It seems to be a problem with my print function, my tripleEach functions seems to work. I haven't tested my reverse function, but it doesn't seem to be working right, but I'm more concerned about the print function. Any help would be appreciated