Swaping of two numbers in c
#include<stdio.h> #include<conio.h> main()
{ int x, y, temp;
printf("Enter the value of x and y ");
scanf("%d%d",&x, &y);
printf("Before Swapping\nx = %d\ny = %d\n",x,y);
temp = x;
x = y;
y = temp;
printf("After Swapping\nx = %d\ny = %d\n",x,y);
getch();
return 0;
}
Swapping of two numbers without third variable
#include<stdio.h> main() { int a, b;
printf("Enter two numbers to swap ");
scanf("%d%d",&a,&b);
a = a + b;
b = a - b;
a = a - b;
printf("a = %d\nb = %d\n",a,b);
return 0;
}
Swap two numbers using pointers
#include<stdio.h> main()
{ int x, y, *a, *b, temp;
printf("Enter the value of x and y ");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
Swapping numbers using call by reference
#include<stdio.h> void swap(int*, int*);
main()
{ int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(&x, &y); printf("After Swapping\nx = %d\ny = %d\n", x, y); return 0;
} void swap(int *a, int *b)
{ int temp; temp = *b;
*b = *a;
*a = temp;
}
Open Visual Studio 2010 by clicking on its program icon. When it loads, select "File/New/Project" and click "Visual C#/Windows Forms Application". A new Windows Form Application project is created, and a blank Windows Form appears in the main editor window.
Locate the panel labeled "Toolbox" which is on either the left or right hand side of the Visual Studio interface. It lists all of the items you can place on your form. Find "ListBox" and "DataGrid".
Click and drag either "ListBox" or "DataGrid" onto your form, and release the mouse button to place the items. Repeat this step for the other item.
Click on the "ListBox" or "DataGrid" that is on your form to enable it to be moved around. Move it close enough so that it connects to the other item. Once you are happy with the placement, you can consider the two elements connected.
Press the green "Play" button to execute the program. A Windows Form appears, with connected "ListBox" and "DataGrid".
Open Visual Studio 2010 by clicking on its program icon. When it loads, select "File," "New," "Project" and click "Visual C#" and "Windows Forms Application." A new Windows Form Application project is created, and a blank Windows Form appears in the main editor window.
Locate the panel labeled "Toolbox," which will be on either the left or right side of the page. The "Toolbox" lists all the available items you can place on a Windows Form. Locate "DataGridView" and "ComboBox."
Click and drag "DataGridView" onto the Windows Form. Release the mouse button to place the item.
Click the small black arrow in the upper right corner of the "DataGridView" on the Windows Form. A menu appears. Click on "Add Column." Press "OK" on the next dialogue box to create a column.
Click and drag the "ComboBox" item from the "Toolbox" onto the column located in the "DataGridView." The ComboBox should fit right inside the column.
Press the green "Play" button to execute the program and test its functionality.
Open a new Visual Basic project. Add two labels, three radio buttons and two button controls to the form. Drag the two labels to the top with Label1 on top of Label2. Drag RadioButton1, RadioButton2 and RadioButton3 to line up vertically beneath the labels. Drag the two buttons beneath the radio buttons with Button1 on the left and Button2 on the right.
Press "F7" to open the code window. Type the following code at the class level:
Dim questions(2, 4) As String
Dim answers(2) As String
Dim quesNum As Integer
The first line creates a two-dimensional array. The first dimension is for each question and the second dimension is for the question itself, three answer choices and the correct answer. The second line creates an array to store the user's answers. The third line creates a counter variable that keeps track of the question the user is on.
Type the following code:
Private Sub GetQuestions()
questions = New String(,) {{"How many colors are in a rainbow?", "5", "6", "7", "7"}, _
{"Who starred in Pirates of the Caribbean?", "Johnny Depp", "John Malkovich", "John Cusack", "Johnny Depp"}, _
{"What is the capital of Florida?", "Miami", "Tallahassee", "Jacksonville", "Tallahassee"}}
End Sub
This subroutine simply initializes the three questions and answers in the questions array. You can add additional questions or get them in other ways, such as through a text file, but if you do, remember to change the size of the question and answer arrays to accommodate the number of questions.
Type the following code:
Private Sub MarkTest()
Dim grade As Integer = 0
For i = 0 To 2
If answers(i) = questions(i, 4) Then
grade += 1
End If
Next
Label1.Text = "Test finished!"
Label2.Text = "You scored " & grade & " out of " & answers.Length & "!"
RadioButton1.Enabled = False
RadioButton2.Enabled = False
RadioButton3.Enabled = False
Button1.Enabled = False
Button2.Enabled = False
End Sub
The first line declares a subroutine that marks the test. It creates a local variable to count the score, then cycles through the answers in the questions array and the answers submitted by the user. For each answer that matches, the grade goes up by one. It then displays the score in the labels and disables the rest of the controls.
Open the Form1_Load() subroutine and type the following code:
Me.Text = "My Multiple Choice Quiz!"
GetQuestions()
quesNum = 1
Label1.Text = "Question " & quesNum & " of " & answers.Length
Label2.Text = questions(0, 0)
Button1.Text = "Previous"
Button2.Text = "Next"
RadioButton1.Text = questions(0, 1)
RadioButton2.Text = questions(0, 2)
RadioButton3.Text = questions(0, 3)
The first line sets the title in the title bar. The next line calls the GetQuestions() subroutine. The third line initializes the question counter variable. The fourth line displays what question number the user is on. The fifth line displays question one in the label. The sixth and seventh lines change the text for the two buttons. The last three lines insert the three multiple choice answers as text for the three radio buttons.
Open the Button1_Click() subroutine and type the following code:
If quesNum > 1 Then
quesNum -= 1
Label1.Text = "Question " & quesNum & " of 3"
Label2.Text = questions(quesNum - 1, 0)
RadioButton1.Text = questions(quesNum - 1, 1)
RadioButton2.Text = questions(quesNum - 1, 2)
RadioButton3.Text = questions(quesNum - 1, 3)
If Button2.Text = "Submit" Then
Button2.Text = "Next"
End If
End If
This is the code for the "Previous" button. It first checks to see if the user pressed the button while already on the first question. If not, it decrements the question counter by one and updates the text for the labels and radio buttons to show the previous question. If the user was on the final question, the text on Button2 changes from "Submit" back to "Next."
Open the Button2_Click() subroutine and type the following code:
If RadioButton1.Checked = True Then
answers(quesNum - 1) = RadioButton1.Text
ElseIf RadioButton2.Checked = True Then
answers(quesNum - 1) = RadioButton2.Text
ElseIf RadioButton3.Checked = True Then
answers(quesNum - 1) = RadioButton3.Text
End If
RadioButton1.Focus()
If quesNum < 3 Then
quesNum += 1
Label1.Text = "Question " & quesNum & " of " & answers.Length
Label2.Text = questions(quesNum - 1, 0)
RadioButton1.Text = questions(quesNum - 1, 1)
RadioButton2.Text = questions(quesNum - 1, 2)
RadioButton3.Text = questions(quesNum - 1, 3)
If quesNum = 3 Then
Button2.Text = "Submit"
End If
Else
MarkTest()
End If
This is the code for the "Next" button. The first seven lines check to see what radio button the user had selected, then saves that answer to the answers array. The next line focuses the radio button selection on RadioButton1. The next line checks to see that the user is not on the final question. If this is true, it increases the question counter by one and updates the labels and radio buttons to show the next question. It then checks to see if the user is now on the final question. If so, it changes the text for the Next button from "Next" to "Submit." If the user was already on the final question and clicked "Submit," the program calls the "MarkTest" function to get the user's score.
Save the Visual Basic program. Press "F5" to run it.
Open Visual Studio 2010 by clicking on its program icon. When it loads, select "File/New/Project," then click "Visual C#/Windows Forms Application". A new Windows Form Application project is created, and a blank Windows Form appears in the main editor window.
Locate the panel labeled "Toolbox" on the left or right hand side of the screen. The "Toolbox" contains items that can be added to your Windows Form. Locate the item "ListBox" and "DataGrid."
Click and drag both "ListBox" and "DataGrid" onto the Windows Form, one at a time. This will place them on your form.
Select the menu item labeled "View." Change the setting to "Code." The main window changes from a design-oriented view to a source code view.
Locate the line of source code labeled "InitializeComponent()" and add the following line of code:
dataGridView1.DataSource = listBox1;
Execute the program by pressing the green "Play" button. A Windows Form appears and displays both a "ListBox" and a "DataGrid." Here, data is bound to each other.
Open Visual Studio 2010 by clicking on its program icon. When it loads, select "File/New/Project "and click "Visual Basic/Windows Forms Application." A new Windows Form Application project is created, and a blank Windows Form appears in the main editor window.
Locate the panel labeled "Toolbox," which should be to the left or the right of the page. The "Toolbox" displays all of the GUI elements you can use. Locate "ListView" from the list of items.
Click and drag "ListView" onto the Windows Form. Release the mouse button to place the item.
Click on the tiny black triangle located on the upper-right corner of the "ListView" item. A menu will appear. Select "Edit Items." A "ListViewItem Collection Editor" window appears.
Select the generic list you wish to associate with this "ListView." Press the "OK" button to accept.
Press the green "Play" button to execute the program. A form appears with a "ListView" displaying the items from your list.
Open Visual Studio 2010. Click "File/New/Project" and click on "Visual C#/Windows Forms Application." This opens a blank Windows Form in the main editor window.
Locate the panel labeled "Toolbox," which is on either the left or right side of the screen. The "Toolbox" contains a list of items that you can add to your form. Locate the item "DataGrid."
Click on "DataGrid" and drag it over to your form. Release the mouse pointer to place the "DataGrid" onto your form.
Right-click on the "DataGrid" and a menu appears. Click on "Add Column." An "Add Column" window appears.
Click "OK" to create a column with the default name of "Column1." The "Add Column" window is still open so you can add more columns if you wish. Alter the text listed under "Name" to change the names of the columns.
Click the green "Play" button to execute your program. A Windows Form appears with a grid of data you made visible on the face of the form.
Open Visual Studio 2010 by clicking on its program icon. When it loads, select "File/New/Project "and click "Visual C#/Windows Forms Application". A new Windows Form Application project is created, and a blank Windows Form appears in the main editor window.
Locate the panel labeled "Toolbox," which should be to the left or the right of the page. The "Toolbox" displays all of the GUI elements that you can use. Locate "ListView" from the list of items.
Click and drag "ListView" onto the Windows Form. Release the mouse button to place the item.
Click on the menu item that says "View" located at the top of the Visual Studio page. A menu appears. Click on "Code" to switch from the visual editor to a source code editor.
Locate the words "InitializeComponent();" which should be located around the middle of the page. After this line, you will place all of the code that prints list items to the "ListView."
Declare a new list of items and initialize it with some values, like this:
List<string> x = new List<string> { "Hello", "World"};
Loop through the list of items in your list using a foreach loop, like this:
foreach (var number in x)
{}
Add the data from the list as a "ListViewItem" by placing the following code within the curly brackets of the "foreach" loop:
ListViewItem item1 = new ListViewItem(number.ToString(), 0);
listView1.Items.Add(item1);
Press the green "Play" button to execute the program. A Windows Form appears and it has a "ListBox," which displays "Hello World."
Access the toolbox tab. Create four text boxes and four labels on the Visual Basic design form. Close the toolbox tab. Arrange these into two columns with labels on the left and textboxes on the right. Access each label's text properties. Change the generic names to descriptive terms such as length, height, area and perimeter. Using straightforward labels avoids later confusion.
Access the toolbox tab again. Add a button to the Visual Basic design form. Access the button's text properties. Change the button's name from the generic option (e.g., Button1) to a descriptive term such as Calculate.
Access the coding window. Add command lines between the Button declaration and the concluding END SUB notation. Use the "Dim" command to declare four different variables. Although variable names can be anything, use the same label names determined in Step 1. Use "As" commands to declare each variable to be either an Integer or Decimal (e.g., Dim length As Decimal).
Add command lines to equate the length and height variables with information found in the appropriate textboxes (e.g., length = Textbox1.Text).
Add a command line for each mathematical function. Declare the area variable to be the calculated product of length and width (e.g., area = length * width). Declare the perimeter variable to be the calculated sum of the rectangle's four sides [e.g., perimeter = ((2 * width) + (2 * length))].
Add a command line to output the area variable's calculated value into its appropriate textbox (e.g., TextBox3.Text = area). Add a final line of code to transfer the perimeter variable's calculated value to its appropriate textbox (e.g., TextBox4.Text = perimeter).
Save the program. Run the program in debugging mode, to verify it is working correctly. This opens a new window. Choose values for length and height and input these into the correct boxes. Click the button to calculate the values for area and perimeter.
Click the Windows "Start" button and select "All Programs." Click "Microsoft .NET Framework," then click "Visual Studio" to open the software.
Double-click the Web file you want to use to convert the session to an integer. Use the following code to create the integer variable for the conversion:
Dim number As Integer
Convert the session variable to an integer. The session variable you convert must contain a numeric value or you receive a syntax error in the code. The following code shows you how to convert the variable:
number = CInt(Session("ID_Number"))
The code above converts the number "ID_Number." Replace this value with your own.
Open your Java file in an editor such as Eclipse, Netbeans or JBuilder X.
Create an interface and two classes that implement the interface by adding the following code above your main function:
interface Fruit {
void callback_method();
}
class Apple implements Fruit {
public void callback_method() {
System.out.println("Callback - Apple");
}
}
class Banana implements Fruit {
public void callback_method() {
System.out.println("Callback - Banana");
}
}
Each class that implements the interface must have a version of the method defined in the interface.
Create a "caller" class that has a method to initiate the callback by adding the code:
class Caller {
public register(Fruit fruit) {
fruit.callback_method();
}
}
In the example, the "register" method can take either an "Apple" or "Banana" as its input and then execute the matching "callback_method" for that version.
Create "Caller", "Apple" and "Banana" objects and then pass both versions of the "Fruit" to the "Caller" object's "register" method, by adding the following code as your main function:
public static void main(String[] args) {
Caller caller = new Caller();
Fruit apple = new Apple(); // Apple version of Fruit
Fruit banana = new Banana(); // Banana version of Fruit
caller.register(apple); // displays "Callback - Apple"
caller.register(banana); // displays "Callback - Banana"
}
This lets you avoid having to create multiple versions of the "Caller" class for each implementation. Any implementation of "Fruit" can be passed to the "Caller," which loads the corresponding callback method for that version.
Save the Java file, compile and run the program to view the callbacks.
Open Visual Studio 2010 by clicking on its program icon. When it loads, select "File," "New," "Project," "Visual C#" and "Windows Forms Application." A new Windows Form Application project is created, and a blank Windows Form appears in the main editor window.
Locate the panel labeled "Toolbox" on the left or right side of the screen. The "Toolbox" lists all of the GUI elements you can place on your form. Locate "ListView" in this list and click on it.
Drag the mouse pointer over onto the Windows Form. Release the mouse pointer to place the "ListView" object. A large rectangle appears on the Form. This is the "ListView" object.
Click "View" from the menu at the top of Visual Studio 2010. A menu appears. Select "Code." The visual editor changes and a source code file appears. This is where you will store the "ListView" items into an array.
Locate the statement labeled "InitializeComponent();", which should be about halfway down the page. All of your code will go after this statement in sequential order.
Declare an integer data type that represents the size of the array you wish to create. Write the following to declare an array of size 100:
int Items = 100;
Create an array of strings, using the variable created in the last step to set its size. To create an array of 100 strings, write the following:
string[] strArray = new string[Items];
Add an item to the list view by writing the following:
ListViewItem item = listView1.Items.Add("String");
Declare an integer data type that will keep track of the index in the array of strings. Write the following:
int i = 0;
Loop through the "ListView" collection using a "foreach" loop, like this:
foreach (var cur in listView1.Items)
{}
Move the data from the "ListView" to the array. You can do this by placing statements within the curly brackets of the "foreach" loop. Every statement thus placed will be executed once every time the loop iterates (so once per item in the list). Write the following four statements within the curly brackets of the "foreach" loop:
strArray[i] = cur.ToString();
System.Console.WriteLine("Program Output:");
System.Console.WriteLine(strArray[i]);
i++;
Execute the program by pressing the green "Play" button. A Windows Form appears and displays the text "String." In the console output window, the following data from the array is displayed:
Program Output:
ListViewItem: {String}
Include the following lines at the beginning of your Java code:
import java.util.Timer;
import java.util.TimerTask;
Create a private class to contain the "run()" method that will be executed when the Timer expires, as in the following sample code:
class Notification extends TimerTask {
public void run() {
System.out.println("The Timer has expired");
}
}
Create a class that starts the timer and contains the private class, as in the following sample code:
public Class TimerTest {
Timer myTimer;
public TimerTest(int seconds) {
myTimer = new Timer();
myTimer.schedule(new Notification(), seconds*1000);
}
class Notification extends TimerTask {
public void run() {
System.out.println("The Timer has expired");
}
}
public static void main() {
new TimerTest(10);
}
}
The amount of time for the Timer is the second parameter to the "Timer.schedule()" method, expressed in milliseconds. The sample code will display the message "The Timer has expired" after 10 seconds; that is, the parameter to the "TimerTest()" constructor.
Open a Visual Basic project. Click on "Project" from the menu, and select "Components." Scroll down to "Microsoft Multimedia Control 6.0," and double-click it to add a check box to the selection. Click "OK" to close the Components window. This adds MMControl to your toolbox.
Double click the MMControl tool in the toolbox to add one to your form. Click and drag the control to a point on the form where you need it.
Open the code window. Type "Dim trackList(4) As String" at the top. This will create an array that will hold five tracks (numbered zero to four). Type "Dim trackNumber As Integer" to create a counter variable.
Open the "Form_Load()" function. Type "trackList(0) = file_path" to load your first track into your array, where "file_path" is the actual location of your MP3 file on your computer. Do this again for "track(List(1)" to "trackList(4)."
Type "MMControl1.Command = "Open"" in the "Form_Load()" function to open the device when the program starts. Type "trackNumber = 0" on the next line and "MMControl1.FileName = trackList(trackNumber)" on the line after that. Type "MMControl.Command = "Play" on the final line in the function.
Open the "Form_Unload" function, and type "MMControl.Command = "Close"" to stop the control when your program ends.
Open the "MMControl1_NextClick" function. Type the following lines:
MMControl1.Command = "Stop"
If trackNumber = 4 Then
trackNumber = 0
Else
trackNumber = trackNumber + 1
End If
MMControl1.FileName = list(trackNumber)
MMControl1.Command = "Open"
MMControl1.Command = "Play"
Open the "MMControl1_PrevClick" function. Type the following lines:
MMControl1.Command = "Stop"
If trackNumber = 0 Then
trackNumber = 4
Else
trackNumber = trackNumber - 1
End If
MMControl1.FileName = list(trackNumber)
MMControl1.Command = "Open"
MMControl1.Command = "Play"
Press "F5" to start your program and listen to your MP3s.
Install and execute at least two different open-source screensavers written in C++. Examples of such programs include Flurry32, Matrix Rain and Kannasaver.
List on paper the changes you'd like to make to each program you try. For example, "The random shapes should flow vertically and horizontally, not just horizontally."
Download the documentation and source code for each screensaver you've made a change list for. Read the documentation, which relates how to compile the screensaver from its source code files.
Install a C++ development kit, such as that from the GNU compiler collection, Open Watcom or Visual C++ Express.
Compile the screensaver's source code, reading the documentation from the development kit and the screensaver for detailed instructions on compilation. Run the resulting executable file to ensure the program compiled correctly.
Print, in a word processor or text editor like Notepad, each source file of the screensaver.
Delete one of the screensaver's source files, then recreate it by typing it in your word processor. Read the source file's printout, made in the previous step, to determine what to type. This step requires a careful reading of the source code, which begins teaching you how the screensaver works.
Recompile and run the screensaver to verify the accuracy of your typing, then delete the source file again. Retype the source file, but this time do so from memory as much as you can. Repeat this step until you type the source file only from memory, then type the screensaver's remaining source files in the same way. This process trains you fully in the screensaver's algorithms and data structures, resulting in the ability to modify the original screensaver.
Write the source code manifesting each of the changes you wrote in Step 2. Compile and debug the modified screensaver, referring to your development kit's documentation on its debugger program to guide you. The result of this step will be a screensaver customized to your design ideas.