Like Tree1Likes
  • 1 Post By whiteflags

Can this basic calculator be simplified? I'm new to programming.

This is a discussion on Can this basic calculator be simplified? I'm new to programming. within the C Programming forums, part of the General Programming Boards category; I am taking a course in C programming at school, I'm still new to programming. I was wondering if there ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    Can this basic calculator be simplified? I'm new to programming.

    I am taking a course in C programming at school, I'm still new to programming. I was wondering if there was a simpler way of programming what I have now. Or just a better way in general.
    And can you sight any mistakes I might have made or might cause a bad habit in the future?

    Code:
    /*Basic Calculator, using two numbers, choosing from four different
    choices, with a basic fail catch only at choice selection.*/
     
    #include <stdio.h>
     
    int main(void)
    {
       int sum,diff,pro,quo,choice,A[1]={0},S[1]={0},M[1]={0},D[1]={0};
       printf("[1] Addition\n[2] Subtraction\n[3] Multiplication\n[4] Division\n");
       scanf("%d", &choice);
     
       switch(choice)
       {
          case 1:
             printf("Finding the sum, enter two numbers: \n");
             scanf("%d %d", &A[0], &A[1]);
             sum=A[0]+A[1];
             printf("The sum of %d and %d is %d\n", A[0], A[1], sum);
             break;
          case 2:
             printf("Finding the difference, enter two numbers: \n");
             scanf("%d %d", &S[0], &S[1]);
             diff=S[0]-S[1];
             printf("The difference of %d and %d is %d\n", S[0], S[1], diff);
             break;
          case 3:
             printf("Finding the product, enter two numbers: \n");
             scanf("%d %d", &M[0], &M[1]);
             pro=M[0]*M[1];
             printf("The product of %d and %d is %d\n", M[0], M[1], pro);
             break;
          case 4:
             printf("Finding the quotient, enter two numbers: \n");
             scanf("%d %d", &D[0], &D[1]);
             quo=D[0]/D[1];
             printf("The quotient of %d and %d is %d\n", D[0], D[1], quo);
             break;
          default:
             printf("Choice not listed!");
             break;
       }
       return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,701
    I haven't looked at simplifying but you do have problems with accessing your arrays out of bounds. You only created arrays with one element yet you are trying to access two elements.


    Jim

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    2
    I thought if I listed an array at 1 for example:
    Code:
    A[1]={0};
    it would have two elements because it starts from 0.

    0 - 1 two elements?
    or is this when when we are talking about inside the array for example:
    Code:
    A[2]={3,1};
    Where 3 is in the 0th place and 1 is in the first place.

    it's making me confused cause I'm trying to store two user-inputs into the Arrays for the selected choice, but if I set the array to:
    Code:
    A[2]={};
    i won't know what to put inside its brackets, for two user-input numbers. Would it still be 0?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,701
    Quote Originally Posted by Christian Jakob View Post
    I thought if I listed an array at 1 for example:
    Code:
    A[1]={0};
    it would have two elements because it starts from 0.

    0 - 1 two elements?
    In C arrays start at zero and end at size - 1. So if you defined your array as A[1] you have room for exactly 1 value, that value would be element zero (A[0]) to get an array that has two elements you would define your array as A[2] which would then have 2 elements and these elements would be A[0] and A[1].
    or is this when when we are talking about inside the array for example:
    Code:
    A[2]={3,1};
    Where 3 is in the 0th place and 1 is in the first place.
    Correct.

    Code:
    A[2]={};
    i won't know what to put inside its brackets, for two user-input numbers. Would it still be 0?
    Yes you could use a single zero, to initialize all the elements to zero or you could use 2 individual values. int A[2] = {3,10}; to initialize your array with the values of 3 and 10. Note you can only use this method when you define your variables. Another option would be to not initialize these arrays int A[2];


    Jim

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    When you declare a static array as you have, the number in the brackets is the number of elements you want the array to have. When you are accessing individual elements the number in the brackets is the index of the element (or the offset from the beginning of the array), so the first element is always index 0.

    There are various ways to declare and initialize arrays:
    Code:
    int arr[2] ; // declares an int array of 2 elements, values are undefined
    
    int arr[2] = { 3, 4 } ; //same as last, but initializes values to 3 and 4
    
    int arr[] = { 3, 4 } ; // same as last, but lets the compiler determine the size from the number of elements initialized.
    
    int arr[2] = {0} ; // declares 2 element int array, but all values are initialized to 0.
    
    int arr[2] = {10} ; // same as last, but 1st element is initialized to 10, and second is probably initialized to 0 - not sure if that is standard or not.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    1
    Quote Originally Posted by Tclausex View Post
    Code:
    int arr[2] = {10} ; // same as last, but 1st element is initialized to 10, and second is probably initialized to 0 - not sure if that is standard or not.
    I think it is standard now, but it can depending on the compiler flags

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,658
    Quote Originally Posted by yakuin View Post
    I think it is standard now, but it can depending on the compiler flags
    Compilers typically have some flags enabled by default which usually amounts to standards support, plus extensions to the C language.

    Still, your compiler would have to be very old to not have this be default behavior.

    Quote Originally Posted by C89 3.5.7 Initialization semantics
    If there are fewer initializers in a list than there are members of an aggregate, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
    In other words, an integer array with too few initializer values would have the rest of its integer elements initialized to 0, like static int.
    Tclausex likes this.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,701
    As far as simplifying your program you could use fewer variables. Each of your switch cases have different variables, you can use the same variables in all your cases. Also when you do integer division there are no fractions so 1/2 will yield zero and you should also check for division by zero before actually doing the calculation.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic calculator
    By Khaled in forum C++ Programming
    Replies: 2
    Last Post: 05-22-2011, 08:17 AM
  2. Basic Sum Calculator
    By ScoutDavid in forum C Programming
    Replies: 14
    Last Post: 09-26-2010, 02:01 AM
  3. A Basic Calculator.
    By bijan311 in forum C++ Programming
    Replies: 4
    Last Post: 12-26-2009, 12:53 PM
  4. Basic Calculator
    By Surge in forum C Programming
    Replies: 18
    Last Post: 12-02-2006, 09:20 PM
  5. need help with my basic c++ calculator
    By iCouch_Potato in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2004, 05:23 PM

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