/* write a program that combines the strings ’dir’,
’subdir’, and ’file’ into a full path: /usr/bin/firefox
You will need to dynamically allocate the path array
to an appropriate capacity.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
char dir[] = "usr";
char subdir[] = "bin";
char file[] = "firefox";
int pathLength = 17;
char *path; /* string */
path = (char*)malloc(pathLength * sizeof(char));
if (path == NULL) { /* always check for NULL after malloc */
printf("malloc failed: Exiting Program!\n\n");
exit(-1);
}
*path = "/";
strcat(path, dir);
strcat(path, "/");
strcat(path, subdir);
strcat(path, "/");
strcat(path, file);
printf(" %s\n", *path);
/* Free the memory assigned to ’word’ */
free(path);
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char dir[] = "usr";
char subdir[] = "bin";
char file[] = "firefox";
int pathLength = 18;
char *path = NULL;
path = (char*) malloc(pathLength * sizeof(char));
if (path == NULL)
{
printf("malloc failed: Exiting Program!\n\n");
exit(-1);
}
*path = '\0';
strcat(path, "/");
strcat(path, dir);
strcat(path, "/");
strcat(path, subdir);
strcat(path, "/");
strcat(path, file);
strcat(path, "\0");
printf("%s\n", path);
free(path);
return 0;
}
strcat(path, "/");
/*
Algorithm findPassFail()
This algorithm reads in a text file containing information on a class and determins if students passed or failed.
It then prints the student number, name, both marks, the overall weighted average, and weither the student passed or failed.
pre:a data file formated as to have the student number (an integer), the student's lastname (a string up to 20
charachters long), the midterm grade (a float between 0 and 100) and the final grade (a float between 0 and 100).
post: It prints the student number, name, both marks, the overall weighted average, and weither the student passed or failed.
return: void
*/
#include <stdio.h>
#include <string.h>
int main (void) {
char fileName[16];
int studentNum = 0;
char studentName[12];
float studentMark1 = 0;
float studentMark2 = 0;
float studentWeightAv = 0;
char passOrFail[5] = "\0";
FILE *studentFile; // file where student data is kept
printf("Enter the file name \n");
scanf("%s", fileName);
studentFile = fopen(fileName, "r");
if(studentFile == NULL) { //this will be true if there was an
perror("failed to open"); //error such as file not existing
return -1;
}
while(!feof(studentFile)){
fscanf(studentFile, "%i%s%f%f", &studentNum, studentName, &studentMark1, &studentMark2);
studentWeightAv = ((studentMark1 * 0.40)+(studentMark2 * 0.60));
if(studentWeightAv > 50)
strcat(passOrFail, "pass\0");
else
strcat(passOrFail, "fail\0");
if(!feof(studentFile)){
printf("%i %s %.2f %.2f %.2f %s \n",
studentNum, studentName, studentMark1, studentMark2, studentWeightAv, passOrFail); // long line broken up
}
};
fclose(studentFile);
return 1;
}
if(studentWeightAv > 50)
strcat(passOrFail, "pass\0");
else
strcat(passOrFail, "fail\0");
#include <stdio.h>
#include <string.h>
int main (void)
{
char fileName[16];
int studentNum = 0;
char studentName[12];
float studentMark1 = 0;
float studentMark2 = 0;
float studentWeightAv = 0;
char passOrFail[5] = "\0";
fileName[0] = '\0';
studentName[0] = '\0';
FILE *studentFile;
printf("Enter the file name \n");
scanf("%s", fileName);
studentFile = fopen(fileName, "r");
if(studentFile == NULL)
{
perror("failed to open");
return -1;
}
while(!feof(studentFile))
{
fscanf(studentFile, "%i%s%f%f", &studentNum, studentName, &studentMark1, &studentMark2);
studentWeightAv = ((studentMark1 * 0.40)+(studentMark2 * 0.60));
passOrFail[0] = '\0';
if(studentWeightAv > 50)
strcat(passOrFail, "pass\0");
else
strcat(passOrFail, "fail\0");
printf("%i %s %.2f %.2f %.2f %s \n",
studentNum, studentName, studentMark1, studentMark2, studentWeightAv, passOrFail);
}
fclose(studentFile);
return 1;
}
printf("%i %s %.2f %.2f %.2f %s \n",
studentNum, studentName, studentMark1, studentMark2, studentWeightAv, passOrFail);
printf("%i %s %.2f %.2f %.2f %s \n",
studentNum, studentName, studentMark1, studentMark2, studentWeightAv, passOrFail);
should be like thisprintf("%i %s %.2f %.2f %.2f %s \n", studentNum, studentName, studentMark1, studentMark2, studentWeightAv, passOrFail);
I guess it just looks a bit weird not to indicate that it's all one line of code.
printf(
"%i %s %.2f %.2f %.2f %s \n",
studentNum, studentName, studentMark1, studentMark2, studentWeightAv, passOrFail
);
/*
Algorithm takeFindPrintMinMaxMarks()
Description : This algorithm asks the user how many marks they want to enter, then to enter that many float marks, calls findMinAndMax to determines the smallest and biggest marks then prints them.
Pre: findMinAndMax.h
Post: The first element of the array to the smallest mark and the second element to the biggest mark. The smallest and biggest marks get printed.
return: 0
*/
#include <stdio.h>
#include "findMinAndMax.h"
int main (void) {
int i = 0;
int size = 0;
float *numArray;
printf("How many marks would you like to enter?\n");
scanf("%i", size); // takes in the size of the array
numArray = (float*)malloc(size * sizeof(float));
if (numArray == NULL) { /* always check for NULL after malloc */
printf("malloc failed: Exiting Program!\n\n");
exit(-1);
}
for(i; i < size; i++){
scanf("%f", numArray[i]); // takes in the marks
}
findMinAndMax(numArray, size); // calls findMinAndMax function
printf("The lowest grade is %.2f and the highest is %.2f\n", numArray[0], numArray[1]);
free(numArray);
return 0; // returns 0.
}
/* *************** findMinAndMax.c ****************
Algorithm findMinAndMax(float numArray[], int size)
Description: This algorithm takes in an array, finds the largest and smallest element and returns the minimum and maximum marks.
Pre: float numArray[] , an array of floats, int size the size of that array.
Post: float max will be the biggest element. float min will be the smallest element. It then sets the first element of the array to the smallest mark and the second element to the biggest mark.
Returns: numArray by ref.
*/
void findMinAndMax(float *numArray, int size){
float max = numArray[0];
float min = numArray[0];
int i = 0;
for(i; i < size; i++){
if( numArray[i] > max)
max = numArray[i];
if( numArray[i] < min)
min = numArray[i];
}
numArray[0] = min;
numArray[1] = max;
}
#include <stdio.h>
#include <stdlib.h>
void findMinAndMax(float *numArray, int size)
{
float max = numArray[0];
float min = numArray[0];
int i = 0;
for(i; i < size; i++){
if( numArray[i] > max)
max = numArray[i];
if( numArray[i] < min)
min = numArray[i];
}
numArray[0] = min;
numArray[1] = max;
}
int main ()
{
int i = 0;
int size = 0;
float *numArray = NULL;
printf("How many marks would you like to enter?\n");
scanf("%i", &size); // takes in the size of the array
numArray = (float*)malloc(size * sizeof(float));
if (numArray == NULL) { /* always check for NULL after malloc */
printf("malloc failed: Exiting Program!\n\n");
exit(-1);
}
for(i; i < size; i++)
scanf("%f", &numArray[i]); // takes in the marks
findMinAndMax(numArray, size); // calls findMinAndMax function
printf("The lowest grade is %.2f and the highest is %.2f\n", numArray[0], numArray[1]);
free(numArray);
return 0; // returns 0.
}
FILE *infile; // File handle for the input file
char filename[100]; // Input filename
TrackingSystem *rTrackingSystem = new TrackingSystem();
int tempNum = 0;
char* tempLoc;
printf("Enter the filename:\n"); // Prompt for the filename
scanf("%s", filename);
// Open the file
infile = fopen(filename, "r");
if( infile == NULL ) {
perror("An error occured opening the input file");
return -1;
}
while(!feof(infile)){ // checks for end of file
printf("Test1:\n");
fscanf(infile, "%i",&tempNum);
printf("%i\n",tempNum);
fscanf(infile, "%s", tempLoc);
//printf("%s\n",tempLoc);
rTrackingSystem->addParcel(tempNum, tempLoc);
printf("Test3:\n");
}
fscanf(infile, "%s", tempLoc);
I've used this exact same code before. It worked then and now it doesn't?
GGGG why isn't this working?Code: [Select]FILE *infile; // File handle for the input file
char filename[100]; // Input filename
TrackingSystem *rTrackingSystem = new TrackingSystem();
int tempNum = 0;
char* tempLoc;
printf("Enter the filename:\n"); // Prompt for the filename
scanf("%s", filename);
// Open the file
infile = fopen(filename, "r");
if( infile == NULL ) {
perror("An error occured opening the input file");
return -1;
}
while(!feof(infile)){ // checks for end of file
printf("Test1:\n");
fscanf(infile, "%i",&tempNum);
printf("%i\n",tempNum);
fscanf(infile, "%s", tempLoc);
//printf("%s\n",tempLoc);
rTrackingSystem->addParcel(tempNum, tempLoc);
printf("Test3:\n");
}
it fails atCode: [Select]fscanf(infile, "%s", tempLoc);
I've used this exact same code before. It worked then and now it doesn't?
2) when you scanf, you put the value in the VARIABLE ADDRESS, ie &variable. you forgot the operator in both scanfs.
BSTNode *BST::insert(BSTNode *v, Parcel *el) {
/**** IMPLEMENT THIS METHOD ****/
printf("Test1:\n");
if (v != NULL && v->getParcel() == el) //there is a node and it already has nData
return NULL;
else if (v != NULL && el->getNumber() < v->getParcel()->getNumber() && v->getLeftChild() != NULL)
return insert(v->getLeftChild(), el);
else if (v != NULL && el->getNumber() > v->getParcel()->getNumber() && v->getRightChild() != NULL)
return insert(v->getRightChild(), el);
else{ //either rNode is NULL or we should insert new node as a left or right child
BSTNode* rNew;
rNew = new BSTNode(el, NULL, NULL);
if (v = NULL) {
printf("Test1:\n");
v = rNew;
}
else if (el->getNumber() < v->getParcel()->getNumber())
v->setLeftChild(rNew);
else
v->setRightChild(rNew);
printf("Test1:\n");
return rNew;
}
}
BSTNode* rNew;
rNew = new BSTNode(el, NULL, NULL);
if (v = NULL) {
printf("Test1:\n");
v = rNew;
:lol :lol :loland yeah, prole is wrong about the need for & here. jumping to conclusions lulz
FM annihilated...
how the hell do I do this?
(r) Assume that the file class list.txt contains data of the following format:
Fname,Lname,Student number,Major,Mark
Write a shell command to:
i. Sort the file alphabetically by the student's last name.
ii. Sort the file in reverse alphabetical order by the student's last name.
iii. Sort the file in order from lowest to highest mark.
iv. Show only the second and fourth columns of the file.
how the hell do I do this?
(r) Assume that the file class list.txt contains data of the following format:
Fname,Lname,Student number,Major,Mark
Write a shell command to:
i. Sort the file alphabetically by the student's last name.
ii. Sort the file in reverse alphabetical order by the student's last name.
iii. Sort the file in order from lowest to highest mark.
iv. Show only the second and fourth columns of the file.
2 dimensional arrays and strcmp?
void Print_Reverse(char* str) {
char* temp;
int iEnd = 0;
int iFront = 0;
temp[0] = str[iEnd];
while(temp[0] != '\0'){ /* finds string size with a loop */
temp[0] = str[iEnd]; /* didn't know if this counts as using it as a varable */
iEnd++;
};
while(iEnd > iFront){
iEnd--; /* goes back on chacter so it does not print the null character */
temp[0] = str[iEnd];
str[iEnd] = str[iFront];
str[iFront] = temp[0];
iFront++;
}
printf("%s\n", str); */
}
I just wrote a 1000 line bash script fml