0

i want make this code to take the number from user in c++

this is an insertion sort code , i want to edit this code so users can input random numbers and sort them. this is the code: .... // sort.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } void insertionSort(int arr[], int n) { int i, j; for (i = 1; i < n; i++) { j = i; while (j > 0 && arr[j - 1] > arr[j]) { swap(&arr[j], &arr[j - 1]); j--; } } } void printArray(int arr[], int size) { for (int i = 0; i < size; i++) cout << arr[i] << " "; } int main() { int arr[] = { 5, 2, 10, 6, 1, 3,8 }; int n = sizeof(arr) / sizeof(arr[0]); insertionSort(arr, n); printArray(arr, n); return 0; }

28th Apr 2018, 9:24 PM
MeDoXs
MeDoXs - avatar
1 Answer
+ 2
Use cin >> varName; to get input Then just use varName (the name of your variable) to access the input
28th Apr 2018, 9:32 PM
Ariela
Ariela - avatar