Array? Who said arr...
 
Notifications
Clear all

Array? Who said array?


Posts: 0
Topic starter
(@Tonakai)
New Member
Joined: 10 years ago

This time is a script wrote in C.
This is a bit more complex than the others,there is a menù build in the script.

We can chose from 4 inputs:
1: Add a value to the array
2: Delete a value from the array
3: Show the array
4: RIP

Script is in C,using PHP to highlight.

//
	//  array.c
	//  cookie method (vBulletin)
	//
	//  Created by Cris on 15/03/2014.
	//  Copyright (c) 2014 Cris. All rights reserved.
	//
	#include 
	#include 
	
	int main()
	{
	    int scelta,pos,num,x,no;
	    int a[10]={0}; //Set Array "a" with 10 elements all at 0.
	    no = 1; //Main loop
	    while (no==1){//Main loop,that's a kind of boolean True or False to keep looping
	
	        printf("Input 1 to inser a value.n");//Menù print
	        printf("Input 2 to delete a value.n");
	        printf("Input 3 to see the array elements.n");
	        printf("Input 4 to quit.n");
	        scanf("%d",&scelta);//Input from 1 to 4
	        if (scelta == 1){//INPUT 1
	            printf("Chose the position of the element from 1 to 10.n");
	            scanf("%d",&pos);
	            printf("Chose the value to insert in position: %d.n",pos);
	            scanf("%d",&num);//Input
	            a[pos]=num;
	            printf("n");//Some spaces
	            printf("n");
	        }
	        else if (scelta == 2){//INPUT 2
	            printf("Chose the position of the number you want to delete from 1 to 10.n");
	            scanf("%d",&pos);
	            a[pos]= 0;//Set the value of chosen element to 0
	            printf("n");
	            printf("n");
	        }
	        else if (scelta == 3){//INPUT 3
	            x=1;//x is a control variable
	            while (x <= 10){//Loop
	                printf("%dn",a[x]);
	                x++;//Increase x,can also write as x=x+1
	            }
	        }
	        else if (scelta == 4){//INPUT 4
	            break;//break function stop the script
	        }
	        else {//If the user didn't chose from 1 to 4 he gets an error
	            printf("Invalid Choice!n");
	            printf("n");
	            printf("n");
	        }//That loop is used to show the menù after every choice.
	    }

If you don't understand just ask!

Reply
Name of the Video Game, and any other Tags