DS learns C

Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Mar 13, 2005
Messages
3,877
Best answers
0
My starting ground is C and I am Majoring Electronic Engineering. I have the choices of C++ or C#. I maybe able to take one as a late class later on sometime though. I could possibly teach myself one language as well.
 
New Member
Joined
Nov 24, 2001
Messages
692
Best answers
0
Ah, ok. If you want to do anything on/with/close to hardware then I guess C++ would be your language of choice, since it runs on many platforms. C# is effectively a windows-only language.
 
Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Mar 13, 2005
Messages
3,877
Best answers
0
Yea, I think C++ was the route most people were going and my teacher said it's easier for most of the teachers there to teach as well. Looking forward to it too.
 

sub

Active Member
💻 Oldtimer
Joined
Jun 18, 2003
Messages
5,961
Best answers
0
Location
New York
Not that I know anything, but I much prefer working in C# compared to C++. It's a shame, though, because the fact that C# is bound to Windows is a deal breaker for me. I've heard that Java is similar to C#, but for some reason I can't motivate myself to start learning it.
 
Member
✔️ HL Verified
🌟 Senior Member
Joined
Oct 16, 2006
Messages
379
Best answers
0
Location
the Netherlands
Not that I know anything, but I much prefer working in C# compared to C++. It's a shame, though, because the fact that C# is bound to Windows is a deal breaker for me. I've heard that Java is similar to C#, but for some reason I can't motivate myself to start learning it.
If you really wanted to you can write multiplatform applications in C# using C# Mono: http://www.mono-project.com/Main_Page
You can also use C# in Unity3D as it uses Mono.
 
Last edited:
Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Mar 13, 2005
Messages
3,877
Best answers
0
Main:
Code:
//
#include "stdafx.h"

//
//the following file contains necessary libs and prototpes
#include "Arith3_L.h"
//
int main(int argc, char *argv[])
{
//declare variables
float tempin,tempout;
int option;
//
MENU:
//
	system("CLS"); //call dos command to keep menu at top and clear
//
	printf("Choose One of the Options Below:\n");
	printf("\t1.Input Pounds to Kilos\n");
	printf("\t2.Input Pounds to Slugs\n");
	printf("\t3.Input Kilos to Pounds\n");
	printf("\t4.Input Kilos to Slugs\n");
	printf("\t5.Input Slugs to Pounds\n");
	printf("\t6.Quit the Program\n");
	printf("\tEnter Corresponding Number Here >> ");
	scanf_s("%d",&option);

//
//do the appropriate conversion
switch (option)
{
case 1: //Pounds to Kilos
        printf("\nEnter the Pounds. ");
        scanf_s("%f",&tempin); 
        tempout = poundtokilos(tempin);
        printf("\nThe Pounds to Kilos is %5.5f\n\n",tempout);
        system("PAUSE");
        goto MENU;
        break;
case 2: //It converts pounds to slugs.
        printf("\nEnter the Pounds ");
        scanf_s("%f",&tempin);  
        tempout = poundtoslugs(tempin);
        printf("\nThe Pounds to Slugs is %5.5f\n\n",tempout);
        system("PAUSE");
        goto MENU;
        break;
case 3: //It converts kilos to pounds.
        printf("\nEnter the Kilos ");
        scanf_s("%f",&tempin);  
        tempout = kilostopound(tempin);
        printf("\nThe Kilos to Pounds is %5.5f\n\n",tempout);
        system("PAUSE");
        goto MENU;
        break;
case 4: // It converts kilos to slugs.
        printf("\nEnter the Kilos ");
        scanf_s("%f",&tempin);  
        tempout = kilostoslugs(tempin);
        printf("\nThe Kilos to Slugs is %5.5f\n\n",tempout);
        system("PAUSE");
        goto MENU;
        break;
case 5: // It converts slugs to pounds.
        printf("\nEnter the Slugs ");
        scanf_s("%f",&tempin);  
        tempout = slugstopound(tempin);
        printf("\nThe Slugs to Pounds is %5.5f\n\n",tempout);
        system("PAUSE");
        goto MENU;
        break;
case 6: //selected Quit option
        printf("\nTerminating! Goodbye!\n\n");
        system("PAUSE");
        break;
default: //not following directions!
         printf("\nYou Did Not Enter Valid Number!\n");
         printf("\nTry Again!\n\n");
         system("PAUSE");
         goto MENU;
         break;
}
//
  return 0;
}

Formula:

Code:
#include "stdafx.h"

//here is the program to convert from C to F
float CenttoFahr(float cent)
{
      float a1,fahr;  //these variables visible only inside
      a1 = (9.0f/5.0f) * cent; // * means multiply
      //note the use of 9.0 instead of just 9 to stress float
      fahr = a1 + 32.0f;
      return fahr;  //this is the output of the function
}
//here is the program to convert from F to C
float FahrtoCent(float fahr)
{
      float a1,cent;
      a1 = (fahr - 32.0f);
      cent = a1 * (5.0f/9.0f);
      return cent;
}
float poundtokilos(float pound)
{
      float kilos;  //these variables visible only inside
	  kilos = pound * 0.453f;
      return kilos;  //this is the output of the function
}
float poundtoslugs(float pound)
{
      float slugs;  //these variables visible only inside
	  slugs = pound * 0.0311f;
      return slugs;  //this is the output of the function.
}
float kilostopound(float kilos)
{
	float pound;
	pound = kilos * 2.2046f;
	return pound;
}
float kilostoslugs(float kilos)
{
	float slugs;
	slugs = kilos * 0.06852176556196105f;
	return slugs;
}
float slugstopound(float slugs)
{
	float pound;
	pound = slugs * 32.1740486f;
	return pound;
}
Header:

Code:
//Arith3_L.h
//
#include "stdafx.h"
//
//Declare the functions used from temperature.c 
float CenttoFahr(float cent);
//
float FahrtoCent(float fahr);
//
float poundtokilos(float pound);
//
float poundtoslugs(float pount);
//
float kilostopound(float kilos);
//
float kilostoslugs(float kilos);
//
float slugstopound(float slugs);
//
 
Last edited:
Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Mar 13, 2005
Messages
3,877
Best answers
0
Code:
int *ArrayReturn(int n)
{
	int i;
	//int arrint[5];
	//int *arrayint = (int *)malloc(5 * sizeof(int));
	int *arrayint = (int *)calloc(5, sizeof(int));

	for (i = 0; i < 5; ++i)
	{
		arrayint[i] = i + n;
	}
	return arrayint;
}

int _tmain(int argc, _TCHAR* argv[])
{
	int i, *x, *y;

	printf("\nFirst time for x\n");

	x = ArrayReturn(2);
	
	for (i = 0; i < 5; ++i)
	{
		printf("\nx[%d] = %d", i, x[i]);
		printf("\n");
	}

	printf("\nFirst time for y\n");

	y = ArrayReturn(4);
	
	for (i = 0; i < 5; ++i)
	{
		printf("\nx[%d] = %d", i, y[i]);
		printf("\n");
	}

	printf("\nSecond time for x after calling function for y");
	printf("\nNote that x is not changed because x and y refer to different arrays");
	printf("\nwhich were allocated in memory each time the function was called\n");


	for (i = 0; i < 5; ++i)
	{
		printf("\nx[%d] = %d", i, x[i]);
		printf("\n");
	}

	//free the allocated resources
	free(x);
	free(y);

	system("PAUSE");
	return 0;
}


I think this is correct.
 
Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Mar 13, 2005
Messages
3,877
Best answers
0
T-T-T-TRIPLE KILL

Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//

void createFile(char *filc)
{
     FILE *fptr;
	 errno_t err;
     int i=1, xn;
     float y,x,a3,a2,a1,a0,xb,xe;
	 printf("\nInput a3, a2, a1, a0: ");
	 scanf_s("%f %f %f %f", &a3,&a2,&a1,&a0);
	 printf("\nInput xstart, xend, and number of points: ");
	 scanf_s("%f %f %d", &xb, &xe, &xn);

     if (err = fopen_s(&fptr,filc,"w") != 0)
	 {
		 printf("File Error: %d", err);
		 system("PAUSE");
		 exit(1);
	 }

     fprintf(fptr,"Evaluation of Function:");
	 fprintf(fptr,"\n");
	 fprintf(fptr,"\n%.4fX^3 + %.4fX^2 + %.4fX + %.4f\n",a3,a2,a1,a0);
     fprintf(fptr,"\nx\ty\n");
     
	 for (i = 0; i <= xn; i++)
     {
		x = xb + (i/float(xn)) * (xe - xb);
        y=(float)(a3*pow(x,3)+a2*pow(x,2)+a1*x+a0);
        fprintf(fptr,"\n%.4f\t%.4f",x,y);
     } 

     fclose(fptr);
     return;
}

//
void readFile(char *filr)
{
	 int i = 0;
     FILE *fptr;
     errno_t err;
     char line1[80];

	 if (err = fopen_s(&fptr,filr,"r") != 0)
	 {
		 printf("File Error: %d", err);
		 system("PAUSE");
		 exit(1);
	 }  
    
	 printf("\n");
	 while (fgets(line1,80,fptr) != NULL)
	 {
		printf("%s",line1);
	 }
	 printf("\n");

     fclose(fptr);
     return;
}

//
int main(void)
{
  char sel;
  char ch = 'a', filnam[20];
  //menu
  MENU:
  printf("Select One of the Following Options:\n");
  printf("\nc = Create a Polynomial File\n");
  printf("\nr = Read the Polynomial File\n");
  printf("\nt = Close Program\n");
  printf("\nYour Selection?__");
  scanf_s("%c",&sel,1);
  //act upon selection
  switch(sel){
  case 'c':
       printf("\nWhat is the File Name?__");
       scanf_s("%s",filnam,20);
       createFile(filnam);
	   printf("\nNew File Created!\n");
	   system("PAUSE");
	   while((ch = getchar()) != '\n' && ch != EOF);
       break;
  case 'r':
       printf("\nWhat is the File Name?__");
       scanf_s("%s",filnam,20);
       readFile(filnam);
	   printf("\nFile Read!\n");
	   system("PAUSE");
	   while((ch = getchar()) != '\n' && ch != EOF);
       break;
  case 't':
	   printf("\nThis Application will now close.\nBye!\n");
	   goto FINISH;
  default:
       printf("\nNot a valid selection.\n");
	   system("PAUSE");
	   while((ch = getchar()) != '\n' && ch != EOF);
       break;
  }

  system("CLS");
  goto MENU;

  FINISH:
  system("PAUSE");	
  return 0;
}
 

Users who are viewing this thread

Top Bottom