PDA

View Full Version : Need help with C Code


eric06
02-03-07, 12:25
I have a teacher in my digital forensics class that also teaches C programming. He wants us to write a program in C. only problem is i have never even looked at C code, i am just now learning Java. So the problem is this. I need to write a method in C that will get INTs from a main method he writes. The Ints are: C, HPC, H, SPT, S, LBA.

Heres the equation: LBA = (((C * HPC) + H ) * SPT) + S - 1;

Heres what i wrote for the code last night, after 3 hours of looking i gave up.

/*
Name: Eric Deering
Date: 2/3/2007
Class: DF 290 M/W/F 11-12
Description: This file will read in the variables for and calculate the size of
a hard drive
*/


{
int C, HPC, H, SPT, S, LBA;

LBA = ((( C * HPC) + H ) * SPT) + S - 1;
printf(LBA);

}

can someone help me get this working and get a main method written to test this out. He wants LBA to be output. Thanks for any help.

ShockTroop
02-03-07, 12:52
Since you're making a method (which I'll call a function) and not the driver, you should format it like this:

void calc(int C, int HPC, int H, int SPT, int S)
{
int LBA;
[calc and display LBA]
}


What you have is fine, you just need to have the function "header" thing (the stuff before the opening bracket).

From your description I'm assuming that your function is being passed in the variables from the driver (what he made), as in the driver is where the ints are inputted, and your function is where LBA is calculated. These ints get passed into your function using the parameters in parantheses.
Void is the return type, and since it's only outputting and not returning anything, void is what to use (if you were returning something, you'd use the datatype you're returning, ie int, double, char, etc). "Calc" can be replaced with whatever name you want, since it's the name of the function.

So it would look something like this:


/*preprocessor commands that refer to header files that contain info needed to make the program work*/
#include <stdlib.h>
#include <stdio.h>

void calc(int,int,int,int,int); //prototype is declared before main

int main() //int because it'll be returning an int to the system when done
{
int C,HPC,H,SPT,S;
[a bunch of scanf's to input, ie printf("Enter varnamehere: "); scanf("%d",varname);]
calc(C,HPCH,H,SPT,S); //passing in parameters to your function
return 0; //stating the program is done and exited successfully.
}

[your function here]


Or you could ditch the function prototype and put your function above main. It just needs to be declared somehow before the program gets to main.

This is a very simple program, so you shouldn't run into many problems other than syntax errors.