Thread: New with C programming. Please help.

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    5

    New with C programming. Please help.

    Hello,I'm fairly new with C programming and I'd appreciate any help. My aim is to be able to "Read a series of integers from a keyboard into a 1-D array,then display on screen. Then ask the user(to enter a number). Then,my code should be able to add the of add the sum from negative to positive(of the number) all inclusive. The array size of the array is 400.
    So far, here is what I have done.When I run the code,it displays only the "Enter a number:" prompt and that's it. I'm yet to add the counter/sum code(which I'm having trouble with) but as said,any help will be appreciated.Thanks.
    Code:
    #include<stdio.h>
    #include<string.h>
    int
    main(void)
    {
     int array[100];
     int i;
     int max;
     int nvalue;
     int sum;
    /*I'm trying to make system print numbers less than and equal to 100 in an array. When that is done it must ask the user for an imput(n),then
    calculate the sum of numbers in the array between –n and n inclusive.*/
    printf("Enter some integers here:"); 
    for (i=0;i<100;++i)
        {
         scanf("%d",&array[i]);
    if(array[i]==-1)
        {
        max=i;
        break;
        }
              max=100;
        }
       for(i=0;i<100;++i)
       {    
         printf("\n%s",array[i]);
       }
    while(nvalue<max)
         {
          nvalue>0;
          nvalue<100;
          scanf("%d");
          printf("Please try again");
    
       /* Lets calculate the sum*/
    for(i=0;i<100;i++)   
          sum=i + nvalue;
          printf("The sum is:%d",sum);
          
          
    return(0);
    } 
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Your code does not match the description of the assignment... Which is clear as mud.

    You already have code in there to calculate a sum of something.

    You stated the array size is 400 but line 6 clearly defines it as 100 as does the rest of the code.

    So I'm left to wonder ... Did you actually write this code yourself?

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    Yes I did..whiles writing the code,I wasn't getting the answers right so I thought It might have been my size.In addition ,I used my notes to help me write this.I have things all over the place as you can tell with the sum code.Anyhow,that's why you see a 100. I guess I should have changed it to avoid that kind of mind set. Guess its back to the drawing board.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mpilot View Post
    Yes I did..whiles writing the code,I wasn't getting the answers right so I thought It might have been my size.In addition ,I used my notes to help me write this.I have things all over the place as you can tell with the sum code.Anyhow,that's why you see a 100. I guess I should have changed it to avoid that kind of mind set. Guess its back to the drawing board.
    Old saying: "Anything worth doing is worth doing right."

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    Quote Originally Posted by CommonTater View Post
    Old saying: "Anything worth doing is worth doing right."
    That's right. I'll keep to that effectively. In the mean time,I know where I went wrong with my coding and working on it.

  6. #6
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    Ok I"m new too but hopefully this helps...I found your code kind confusing but maybe with some work we can get it to work. 1st I would pay more attention to your brackets.... you just seem to place them all over randomly. and I would read up on your for loops and if statements so you can make some more clearer code.

    If Statements in C - Cprogramming.com Tutorial <---if statements
    For, While and Do While Loops in C - Cprogramming.com<---- for loops


    Code:
    #include<stdio.h>
    #include<string.h>
    int
    main(void)
    {
        int array[100];
        int i;
        int max;
        int nvalue;
        int sum;   // this can be changed into int array[100],i,max,nvalue,sum;
      
      /*I'm trying to make system print numbers less than and equal to 100 in an array. When that is done it must ask the user for an imput(n),then
        calculate the sum of numbers in the array between –n and n inclusive.*/
        printf("Enter some integers here:");
        for (i=0; i<100; i++)
        {
            scanf("%d",&array[i]); // um try moving his after print.... I thnk don't get this forloop
        }//missing bracket!!!
        if(array[i]==-1) // what kind of test is this if statment? and what is the point of it?
        {
            max=i;
            break;
        }
        max=100;
    //}um random bracket?
    for(i=0; i<100; i++)
    {
        printf("\n%s",array[i]);//um I don't get this for loop ...
        //so you want a empty row (/n) and then a string (%s) ???
    
    }
    while(nvalue<max) //is it nessary to make a mox = 100? or could you just do nvalue <100
    {
        nvalue>0;
        nvalue<100; // try making your white an if stament that inclues this with a test
        scanf("%d");// what are you tryig to scan here?
        printf("Please try again");
    } //missing bracket!!!
        /* Lets calculate the sum*/
        for(i=0; i<100; i++)
    
        {//your missing a bracet.... and this for loop is also confusing....
            sum=i + nvalue;
        printf("The sum is:%d",sum);
        }
    
        return(0);
    // }why do you have two brackets here?
    }
    Last edited by Cess; 11-20-2011 at 04:46 PM.
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About Device Driver Programming And Socket Programming
    By pritesh in forum Linux Programming
    Replies: 6
    Last Post: 01-23-2010, 03:46 AM
  2. Replies: 1
    Last Post: 08-19-2007, 03:55 AM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 11:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM

Tags for this Thread