S
sajjad
Hello friends
I do not got Malloc to work
Please take a
look at this source code and tell me whats wrong. I have marked the 2
problem areas.
/* This program will sort an array of floats from the user.*/
#include <stdio.h>
#include <stdlib.h>
/* prints the contents of the array to stdout. array is the array
to print, size is it's size */
void prn_array(float *array, int size) {
int loopcount; /* used as a loop counter */
printf("\nThe sorted array is:\n");
for (loopcount=0; loopcount < size; loopcount++)
printf("%d ",array[loopcount]);
printf("\n");
}
/* swaps two floats from the array. num_one and num_two are the floats */
int swap( float *num_one, float *num_two) {
float temporary; /* temporary variable */
temporary = *num_one;
*num_one = *num_two;
*num_two = temporary;
}
/* sorts array in descending order. array is the array
to print, size is it's size */
void sort_descending (float array[], int size) {
int i, j; /* integers */
for (i=0; i<size - 1; ++i)
for (j= size-1; i<j;--j)
if (array[j-1] < array[j])
swap(&array[j-1], &array[j]);
}
/* sorts array in ascending order. array is the array
to print, size is it's size */
void sort_ascending (float array[], int size) {
int i, j; /* integers */
for (i=size; i>0 - 1; --i)
for (j= 0-1; i>j;++j)
if (array[j-1] > array[j])
swap(&array[j-1], &array[j]);
}
main() {
int size; /* number of floats in the array */
int *pointer, loopcount, choice; /* pointer is an int pointer,
loopcount is used as a loop counter, choice denotes users choice */
float array[size]; /* <---------------------- Problem one */
/* A welcome message to the user*/
printf("\nWelcome in this program
you will enter an array of floats and the program will sort it in
ascending
or descending order and and finally print it");
/* asking for the numbers of floats in the array */
printf("\nHow many numbers would you like sorted? ");
/* read user input and place into size variable */
scanf ("%d, &size");
array= (float *)malloc(size * sizeof(float)); /* <----------- Problem
two */
/* asking for the array */
/* branch based on size entered */
if(size==1)
printf("\nPlease enter 1 number");
else
printf("\nPlease enter %d numbers");
for (loopcount=0;loopcount<size; loopcount++)
scanf("%f", array[loopcount]);
while (choice==0){
printf("\n Please choose one of the following:");
printf("\n 1. Sort in ascending order ");
printf("\n 2. Sort in descending order");
printf("\n Enter choice --> ");
scanf ("%d", choice);
if ((choice==1)||(choice==2))
break;
}
/* which sort to use, branch on choice */
if(choice==1)
sort_ascending(array, size); /* sort the array in descending order */
else if (choice==2)
sort_descending(array, size); /* sort the array in descending order
*/
/* output the contents of the array */
prn_array(array, size);
}
Please help
I do not got Malloc to work
Please take a
look at this source code and tell me whats wrong. I have marked the 2
problem areas.
/* This program will sort an array of floats from the user.*/
#include <stdio.h>
#include <stdlib.h>
/* prints the contents of the array to stdout. array is the array
to print, size is it's size */
void prn_array(float *array, int size) {
int loopcount; /* used as a loop counter */
printf("\nThe sorted array is:\n");
for (loopcount=0; loopcount < size; loopcount++)
printf("%d ",array[loopcount]);
printf("\n");
}
/* swaps two floats from the array. num_one and num_two are the floats */
int swap( float *num_one, float *num_two) {
float temporary; /* temporary variable */
temporary = *num_one;
*num_one = *num_two;
*num_two = temporary;
}
/* sorts array in descending order. array is the array
to print, size is it's size */
void sort_descending (float array[], int size) {
int i, j; /* integers */
for (i=0; i<size - 1; ++i)
for (j= size-1; i<j;--j)
if (array[j-1] < array[j])
swap(&array[j-1], &array[j]);
}
/* sorts array in ascending order. array is the array
to print, size is it's size */
void sort_ascending (float array[], int size) {
int i, j; /* integers */
for (i=size; i>0 - 1; --i)
for (j= 0-1; i>j;++j)
if (array[j-1] > array[j])
swap(&array[j-1], &array[j]);
}
main() {
int size; /* number of floats in the array */
int *pointer, loopcount, choice; /* pointer is an int pointer,
loopcount is used as a loop counter, choice denotes users choice */
float array[size]; /* <---------------------- Problem one */
/* A welcome message to the user*/
printf("\nWelcome in this program
you will enter an array of floats and the program will sort it in
ascending
or descending order and and finally print it");
/* asking for the numbers of floats in the array */
printf("\nHow many numbers would you like sorted? ");
/* read user input and place into size variable */
scanf ("%d, &size");
array= (float *)malloc(size * sizeof(float)); /* <----------- Problem
two */
/* asking for the array */
/* branch based on size entered */
if(size==1)
printf("\nPlease enter 1 number");
else
printf("\nPlease enter %d numbers");
for (loopcount=0;loopcount<size; loopcount++)
scanf("%f", array[loopcount]);
while (choice==0){
printf("\n Please choose one of the following:");
printf("\n 1. Sort in ascending order ");
printf("\n 2. Sort in descending order");
printf("\n Enter choice --> ");
scanf ("%d", choice);
if ((choice==1)||(choice==2))
break;
}
/* which sort to use, branch on choice */
if(choice==1)
sort_ascending(array, size); /* sort the array in descending order */
else if (choice==2)
sort_descending(array, size); /* sort the array in descending order
*/
/* output the contents of the array */
prn_array(array, size);
}
Please help