Thread: Mini program that i can't solve (one-dimensional)

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    1

    Mini program that i can't solve (one-dimensional)

    Hi, I don't know how to do a mini program due to lack of knowledge.
    The condition is like this:

    There is one-dimensional array a[1..n], n<=50 of int numbers.
    I need to calculate the sum of odd numbers from even positions.

    Can somebody help me ?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <math.h>
    void main()
    {
      int n,i;
     float a[50],sum;
      printf("Number of elements\n");
        scanf("%d",&n);
      printf("Elements:\n");
      for(i=0;i<n;i++)
      {      printf("a[%d]=",i);
          scanf("%f",&a[i]);
      }
    
    
    And here what ???
    
    
    
    
    }
    Thank you

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    1. For testing after inputing the numbers it would be a good idea to output (printf) the numbers.
    2. I would next try to output just the even positions
    3. Then, I would output just the odd numbers in the even positions.
    4. Last I would add those numbers.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    A few things to start with

    • Make sure your code is neatly formatted and indented, as below, to make it easy to read. If it's easy to read, it's harder to make mistakes, and easier to find and fix them when you do.
    • Don't include conio.h. It's very outdated, non-standard, and you aren't using anything it provides anyway.
    • You don't actually need math.h for this, don't include that either.
    • You don't really need stdlib, but I will suggest you keep it. Then, you can use return EXIT_SUCCESS; at the end of main instead of return 0;. They behave the same, but using EXIT_SUCCESS is much clearer. Clear code is A Good Thing.
    • void main is wrong. Read this: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com.
    • Don't use magic numbers like 50. Instead, define a constant with a descriptive name, like MAX_ELEMENTS
    • Give variables more descriptive names. n should be num_elements. a could be elements. i is fine, it's a basic loop counter/array index, which is one of the few cases where single-letter variable names are a good idea.
    • If you have restrictions on input values, such as < 50 elements, please validate the user's input. That is, if they enter 51 or 500, display an error and ask them again. A do-while loop is good here since it always executes once, and you always want to give the user a chance to input first. If you don't validate their input, your program (as currently written) will attempt to store data in array elements that don't exist. This will access memory your program doesn't own, which results in undefined behavior (see this link), meaning anything, or nothing, can happen. Though most likely, your program will crash with a seg fault.
    • Small nit, when asking the user for input, use language that suggests they should do something. "Number of elements" sound like you (the program) will tell the user something. "Please enter the number of elements (max %d): " is much better. Note the %d will be filled in by providing the comment.
    • Generally it is better to use double, it offers more precision. Only use float if you really, really need to save memory, which does not seem to be the case for you.


    Implementing the above (leaving a bit of work for you to do in a few places.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define MAX_ELEMENTS 50
    
    int main(void)
    {
        int num_elements, i;
        double elements[MAX_ELEMENTs], sum;
        do {
            printf("Please enter number of elements (max %d): ", MAX_ELEMENTS);
            scanf("%d", &num_elements);
            if (num_elements < 1 or > 50)
                print error, please input a value between 1 and MAX_ELEMENTS
        } while (num_elements is < 0 or > 50);
    
        printf("Enter elements:\n");
        for(i=0;i<n;i++)
        {      printf("Please enter a[%d]=", i);
            scanf("%lf",&a[i]);  // <-- note, you must use %lf to read in a double with scanf, though you can print it with just %f (same as float) using printf
        }
    
         // And here what ???
    
    
        return EXIT_SUCCESS;
    }
    Okay, now we can get to the "And here what ???" part.

    When solving a programming problem, you should work stuff out on paper first. Start with the requirements in English (or your native language). Figure out how you, a human, would solve that problem. Pay attention to the process, every little calculation -- it will form your algorithm. Then, implement each little step in your program. Let's look at your requirements:

    1. sum of odd numbers
    2. from even positions

    Even numbers implies there is no remainder when you divide the number by 2, so we must find a "remainder" operator, also known as a "modulus" operator. Do you know which operator that is? Check your class notes or textbook, or Google "C operators" if you're not sure.

    1. Sum the odd numbers. So you need to check if the contents of each array spot are odd.
    2. Even positions. You need to check if the array index is an even number.

    If both of those conditions are true, then you can add that array spot to the sum. Note, you should make sure your sum starts with a sensible value -- recall what you have (probably) learned about initializing variables. Note I emboldened the word "if", probably for a good reason.

    EDIT: Note that, if you must sum only odd numbers, it implies that the array must contain integers. A floating point number (i.e. non-integer) does not really have the concept of odd and even.
    Last edited by anduril462; 01-28-2015 at 12:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. plz help me to solve this c++ program
    By umar123 in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2011, 06:00 AM
  2. a bug in my mini program!
    By Perspolice1993 in forum C Programming
    Replies: 3
    Last Post: 09-28-2011, 09:51 PM
  3. Need help to solve this program
    By kitymarine in forum C Programming
    Replies: 2
    Last Post: 06-20-2011, 11:10 PM
  4. can u solve this program :P
    By bawen in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2006, 12:16 PM
  5. Replies: 2
    Last Post: 12-19-2005, 06:57 PM