hey all,
new here and new to coding in general.
I've got two problems
First, I'm working on a project that requires us to make a program that can converts several different distance types, the program needs to:
-Have the user input a distance with units (either m(meters),M(miles),km(kilometers),ft(feet))
-the program needs to read the number and the units and perform the appropriate conversion(meters-->kilometers,ft-->miles, and vice versa)
-the program then needs to display the result with the new units (of course!)
for example: input= 1000 m output= 1 km
I think I'm grasping this pretty well, but im having trouble getting scanf to read both the number and units correctly. I believe I may need to utilize pointers, but my knowledge here is pretty damn shalllow. I've tried all sorts of combinations of code, i either get a compiler error, something like: incompatible types in assignment, function not defined, assignment of read only location. These vary depending on the nuances i change. If it does manage to compile, then usually I get a segmentation fault once i enter in a value.
so anyway, I'm just trying to get one block of code to work. I should be able to go in later and put in all the if and else statements needed to specify the different conversions.
the code
Code:
#include <stdio.h>
main() {
/* declare main program variables */
int number;
char unit[2];
int ConvNum;
printf("Please enter the value that you wish to convert (including units):");
scanf("%d %s", &number,unit);
if(unit="%s","m"){
ConvNum=number*.001;
printf("%d meters is equal to %d Miles",number,ConvNum);
return 0;
}
}
also, our other project requires us to make a program that
A.)Reads data from an array
A.)reorders the array in either ascending or descending order (users choice) and writes the new array to a new file, specified by the user
B.)Prints first, last, and median number after reorder
the problem I'm having with this one is that the program seems to be adding a random, very large, number in the final array (the reordered one). This of course changes the total number of elements in the array, which then changes the values for median, as well as first and last number.
heres the code for that one
Code:
/*Header files that the program uses to interpret the code*/
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
/*The main function of the program*/
main() {
/* declare main program variables */
char filename[256];
FILE *in_fd;
FILE *out_fd;
int count = 0;
float next_number;
float InputNumbers[100];
int i=0;
int j;
int mid;
float median;
float temp;
int max;
float ascending[100];
int Temp;
char outputname[256];
char order[256];
/* get name of input file from user */
printf("Enter input file name:");
scanf("%s",filename);
/* open file and print error message and exit on failure */
if ((in_fd = fopen(filename, "r")) == 0) {
fprintf(stderr, "File: %s Not Found!\n",filename);
exit(0);
}
/*Asks user to name the ouput file, storing it in "outputname"*/
printf("Enter the name of your output file:");
scanf("%s", outputname);
/*Creates output file, and opens it for writing*/
out_fd=fopen(outputname, "w");
/* this while loop reads numbers from the file until it is empty. */
/* It stores these numbers in "InputNumbers" as "count" increments */
/* through the array */
while(fscanf(in_fd,"%f\n",&next_number)!=EOF)
{
count++;
InputNumbers[count]=next_number;
}
max=count;
count=0;
/*Asks the user to enter either 1 or 2 in order to select an operation*/
printf ("For ascending order, enter 1\nFor descending order, enter 2\n");
scanf ("%d",&j);
/* This "if" statement directs the program to the appropriate "for" statement */
if (j==1){
/*This is a bubble sort. The program increments through the array, */
/*comparing each set of adjacent numbers. It swaps the numbers in */
/*order to satisfy the inequality*/
for (count=0;count<=max-1;count=count+1)
{
for(i=0;i<=max-1;i=i++)
{
if(InputNumbers[i]>InputNumbers[i+1])
{
Temp=InputNumbers[i];
InputNumbers[i]=InputNumbers[i+1];
InputNumbers[i+1]=Temp;
}
}
}
}
/*This is also a bubble sort, but reversed in order to find the descending order*/
else if (j==2)
{
for (count=0;count<=max-1;count=count+1)
{
for(i=0;i<=max-1;i=i++)
{
if(InputNumbers[i]<InputNumbers[i+1])
{
Temp=InputNumbers[i+1];
InputNumbers[i+1]=InputNumbers[i];
InputNumbers[i]=Temp;
}
}
}
}
/*If the user enters a choice other than 1 or 2, the program informs the user */
/*of his/her mistake*/
else
{
printf("Not a valid choice");
}
/*If the number of elements in the array is even, then the program devides the max */
/*by 2 and assigns it to "mid". It then adds the middle number ("mid") to the number */
/*one less than the middle number, and then devide by 2*/
if (max%2==0){
mid=max/2;
median=(InputNumbers[mid-1]+InputNumbers[mid])/2;
/*This "for" loop increments through the now reordered array and places the values*/
/*into the output file defined by the user. It then displays the other values within*/
/*the display window*/
for (i=0;i<=max;i++)
{
temp=InputNumbers[i];
fprintf(out_fd,"%f\n",temp);
}
printf("First number is %f\n",InputNumbers[0]);
printf("Last number is %f\n",InputNumbers[max]);
printf("The Median is %f\n",median);
}
/*If the max is not even, then the program devides max by two and uses the value*/
/*at that address as the median. Because mid is an "int" type rather than float,*/
/*the number is rounded to the nearest integer. Because an odd integer devided by*/
/*two always has a ".5", the number will round up to what is the appropriate median*/
else{
mid=max/2;
for(i=0;i<=max;i++)
{
temp=InputNumbers[i];
fprintf(out_fd,"%f\n", temp);
}
printf("First number is %f\n", InputNumbers[0]);
printf("Last number is %f\n", InputNumbers[max]);
printf("The Median is %f\n" ,median);
}
close(in_fd);
close(out_fd);
}
also, feel free to correct any mistakes I make in the comments; those are graded as well, so if you notice something interpreted wrong, feel free to let me know!
Thanks a ton!