Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- /*
- loops and condistionals,
- how to get out of a loop
- using them
- */
- int main() {
- char run='y';
- int a=0,b=0,sum=0,ct=0;
- int arrayFOReveryThing[2];
- while(run == 'y'){
- /*
- request input for
- processing
- */
- printf("Enter an integer amount\n");
- scanf("%d", &a);
- /*just killing 2 birds with
- one stone using assignment
- printf to see what ct is doing */
- printf("1: in loop ct=%d\n",ct);
- arrayFOReveryThing[ct]=a;
- printf("Enter another integer amount\n");
- scanf("%d",&b);
- printf("2: in loop ct=%d\n",ct);
- arrayFOReveryThing[++ct]=b;
- printf("Would you like to "
- "change the values? y/n \n");
- scanf(" %c",&run);
- /* using a condisinal for
- stopping the loop or not
- in case anything other than y
- was inputed thats user error,
- stop the loop anyways */
- if(run != 'y')
- run='n';
- else
- ct=0; // reset your counter
- printf("3: in loop ct=%d\n",ct);
- } // while loop
- printf("array el 0=%d\n", arrayFOReveryThing[0]);
- printf("array e2 1=%d\n", arrayFOReveryThing[1]);
- /* printing out the sum in your
- printf function */
- printf("Total is %d\n",(a+b));
- /* doing the math ahead of time */
- sum=a+b;
- printf("sum = %d\n",sum);
- /*reset sum to zero because
- it is being used again*/
- sum=0;
- /* using an array because you can
- and maybe you want to pratice
- arrays and loops
- lets not forget arrays are zero
- based */
- /* with a loop */
- printf("ct = %d\n",ct);
- /* add 1 to ct because
- it was pre Incremented
- inside of array element
- to move it up 1 */
- for(int i=0;i<ct+1;i++)
- sum+=arrayFOReveryThing[i];
- printf("sum = %d\n",sum);
- /*using printf function,
- knowing which elements
- hold the values */
- printf("sum = %d\n",arrayFOReveryThing[0]+
- arrayFOReveryThing[1]);
- /* knowing where and how you
- can break off a line of
- code to put it on
- the next line down. not
- having to rely on word wrap
- in your IDE and why it still
- compiles is a good
- thing to know as well.
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement