2 questions, (newbie needs help)

This is a discussion on 2 questions, (newbie needs help) within the C Programming forums, part of the General Programming Boards category; Hi, I have just began to learn C. I have familiarized myself with the basic layout of a very small ...

  1. #1
    Unregistered
    Guest

    2 questions, (newbie needs help)

    Hi, I have just began to learn C. I have familiarized myself with the basic layout of a very small C Program. So far, all I know how to do is write things (eg: Hello World) and enter variables and do basic math ( + - * / ). I was wondering how you do more advanced math like Square Roots and Powers and such. I read on a site something to do with a Header file and some other stuff. Could someone give me the source for a header file and a program that lets u enter a number and then squares the number.

    My second question, I already know how to make people type things and have these typed things become variables. However I have only got this to work with numbers! How can u make a word a variable? Once again, the source of a very basic program (such as, type in a name, and then display the name) would be more sufficient than trying to explain it to me.

    Thanks - Dave.

  2. #2
    Unregistered
    Guest
    The header file for advanced mathematical functions is math.h, surprise

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(){
      int num=10;
      int result;
      
      result = sqrt(num);
      printf("%d", result);
    
      return 0;
    }
    Making a word a variable requires the use of an array, referred to as a string in this case. There's not a string data type, so you have to use a char array.
    Code:
    #include <stdio.h>
    
    int main(){
      char array1[50];
      char array2[50];
    
      printf("Enter your first name: ");
      scanf("%s", array1);
      printf("Your first name is %s\nEnter your whole name: ", array1);
      scanf("%[^\n]", array2);
      printf("Your whole name is %s", array2);
    }
    The first scanf will allow you to enter a single word, but as soon as it reads in a space it will stop. The second scanf will read until it sees a newline, meaning it will read into the array until it reaches the point where you hit enter.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    square root
    Code:
    #include <math.h>
    
    main()
    {
    int num;
    double answer;
    
    printf("Type in the number: ");
    scanf("%d",&num);
    
    answer=sqrt(num);
    
    printf("The sqare root of %d is %f",num,answer);
    }
    #include <math.h> <-----This is how you add a header file


    Code:
    main()
    {
    char name[20];
    
    printf("Please type in your name: ");
    gets(name);
    
    printf("Your name is %s",name);
    }
    Good luck

    -Thantos

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    First question: Just include math header file
    Code:
    #include <math.h>
    Second question: You have to you get the string from a user with the fgets function. Here's how you would implement that
    Code:
    char line[100]; /* variable to hold the string */
    
    int main()
    {
        printf("Please enter your name: ");
        fgets(line, sizeof(line), stdin);    /* This puts the string in line */
        line[strlen(line)-1] = '\0';  /* Replaces the newline (\n) */
    
        if (line == "John")
            printf("You wrote John\n");
        else
            printf("You didn't write John\n");
    
        return (0);
    }
    The reason that I did this

    > line[strlen(line)-1] = '\0'; <

    Was because that if I didn't delete the newline (\n) character, then the string would look like this:

    > line = {'J', 'o', 'h', 'n', '\n', '\0'); <

    So, that is why you have to change it to '\0' so it is read right.

    > line = {'J', 'o', 'h', 'n', '\0', '\0'}; <

    So, now the if statement will print the right result (well, it will be printed right, just not the way you thought it would. C is NEVER wrong. Only the programmer).

    Does this solve your problem?
    1978 Silver Anniversary Corvette

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Sorry, when I started writing the reply, nobody had replied already. And then I finish up and three others have replied with the correct answer. Sorry.
    1978 Silver Anniversary Corvette

  6. #6
    Unregistered
    Guest
    Thank you all. This solves my problem.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    100

    Powers

    For the powers:

    Code:
    #include <iostream.h>
    #include <math.h>
    #include <conio.h>
    
    main()
    {
     int x=3, y=4;
     cout<<pow(x,y)<<endl;
     getch();
     return 0;
    }
    this should work for 'em
    Sauron a few seconds before his defeat:
    "What? A 'division by 0' error?!?"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory handling functions (newbie questions)
    By PaulBlay in forum C Programming
    Replies: 6
    Last Post: 03-10-2009, 07:37 AM
  2. newbie questions
    By raptorx in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 10:30 PM
  3. Im a newbie to C and i have some questions
    By pave1104 in forum C Programming
    Replies: 5
    Last Post: 07-05-2006, 10:48 PM
  4. Real newbie with VC6 questions
    By MagiZedd in forum Windows Programming
    Replies: 8
    Last Post: 10-15-2001, 09:27 PM
  5. newbie questions again
    By dune911 in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2001, 03:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21