Computer Science Homework Help

West La College Single Loop C Coding Computer Science Project

 

Assignment #1

For your first project, you will be doing the same thing you will be doing when you have been introduced to the basics of programming, string manipulation. your assignment is to write a program which converts numbers from decimal to Hex, binary, and Octal. Your program should behave in a similar way as any regular command in a terminal environment.

You need to cover the following

Simply call on the program, give it a parameter and press enter. The conversion for that specific number should then be displayed unless no arguments were given. If by chance you do not provide the program with any arguments, then the program should display a user prompt, prompting the user to enter a decimal number.

UNDER NO CIRCUMSTANCES ARE YOU ALLOWED TO WRITE A SINGLE LOOP!

So, how would we approach such a task?

Recall the structure of your typical C code:

#include <stdio.h>

/* comment. */

int main(int argc, char *argv[])

{

// comment

printf(“Hello world”)

return 0;

}

Note that main is a function but it takes some arguments.

int main ( int argc, char *argv[] )

These arguments are information about a string which you can pass whenever you run your program.

First, compile your code:

gcc -o Aassign1 Assignment1.c

Then pass in the arguments whenever you execute it

./Aassign1 arg1 arg2 arg3 arg4 arg5

Now, lets go back to the main method:

int main ( int argc, char *argv[])

argc is the number of arguments passed to your executable, while argv is an array of strings of every word (represented as a char array) separated by space.

Your first argument by default is the file name.

So you can easily write something like:

#include <stdio.h>

int main(int argc, char *argv[])

{

int i;

if (argc == 1)

{

printf(“You did not provide a number to convert”);

// prompt the user for a number the old fasion way

}

else () // You have at least one extra argument

{

}

return 0;

}

What do I want?

I need you to write a program that takes in 0 or 1 arguments.

• If you choose to provide an argument the only print out the conversion results

./Aassign1 14

• If you don’t provide the user with an input then, you have to prompt the user for a number

./Aassign1

Enter a decimal number for conversion:__

Loops

YOU UNDER NO CIRCUMSTANCES ARE ALLOWED TO WRITE A SINGLE LOOP!

Bases to cover:

I want you to be able to be able to convert numbers from Decimal to Octal, Hex, and Binary.

The structure of your main computational methods should look something like this, however, you may add more helper functions if you so desire:

char * decimalToOctal (…)

char * decimalToBinary (…)

char * decimalToHex (…)

void seekUserInput (…)

*** inputValidation (…)

int main (…)

Remember that arrays cannot be passed around if they are allocated on the stack if the stack frame they are in gets popped off. make sure to allocate them onto the heap. Don’t forget to free your memory too, jack!

Oh no! This is too much work! What about conversion!

The kewl way of doing this is to actually use libraries. The Standard C libraries

You can read more about strings in C.

But what you should likely look into are strcpy, sprinf, stroll,and strtrok.

If you want more tutorials you can check the following page as well.

CODE

Example

CODE That has been submitted multiple times over multiple semesters

Example 1

DEDUCTIONS

You will be deducted points if any of the following things occur:

(-6 points) If I see a single loop

(-5 points) If a fixed string size is used for the results. String sizes need to depend on the input!

(-8 points) if multiple people not in your group submit the same file.

(-8 points) Doesn’t compile in GCC [linux version not CLANG].

(-3 points) if you write everything as a single main function or have a method that exceeds around 10 to 15 lines.

(-10 points) if it doesn’t work according to the provided specification