C :: Program That Sorts Array Of 5 Integer Numbers
Oct 6, 2013
Write a C program that sorts an unsorted array of 5 integer numbers in ascending order by swapping numbers repeatedly. Specifically, first prompt the user to input 5 numbers and store the numbers in an array. Then, if the numbers in the array are not in ascending order, ask the user to provide the indices of two numbers in the array that the user wants to swap. If the user does not enter valid indices (i.e., outside 0 to4), then re-prompt the user to enter new indices. Continue prompting the user to provide indices of numbers to swap until the array becomes sorted in ascending order. Finally, when the array becomes sorted, the program should print "The array is now sorted in ascending order".
Implementation Requirements
Use the #define directive to define the size of the array.
Implement a function called "checkArrayOrder" to check whether an array is in ascending order or not. The prototype of the function is
"intcheckArrayOrder(int arr[], int size);". The function should return
1 if the array passed to the function is in ascending order0 if the array passed to the function is not in ascending order
Define the function prototype in your program.
Implement a function called "swap", whose prototype is "int swap(int arr[], int size, int i, int j);" which swaps the numbers in positions "i" and "j" of the array passed to the function and returns
1 if the indices"i" and "j" are within bounds (i.e., between 0 and 4) and the swap operation is performed correctly.
0 if the indices"i" and "j" are outside bounds so that the swap operation cannot be performed.
Define the function prototype in your program.
Use the function "checkArrayOrder" whenever your program needs to check whether an array is in ascending order or not; and the function "swap" to swap the numbers the user specifies (Note that, you should check whether the user provided indices are valid inside the function "swap" and not inside "main").
here is my code so far
Code:
#define ARRAY_SIZE 5
void main()
{
int i, array[ARRAY_SIZE];
printf("Enter %d numbers
", ARRAY_SIZE);
for(i=0; i<ARRAY_SIZE; ++i)
scanf("%d", &array[i]);
[Code] .....
I'm just not sure how to proceed. I'm not really sure how to progress the checkArrayOrder function to the swap function (in the if statement).
View 7 Replies
ADVERTISEMENT
Jul 5, 2014
I have to write a program that sorts numbers from least to greatest. The way that it has to sort them is:
1) The program assigns the first number to be a minimum
2) If the next number is less than the minimum is now that number and they switch places. (We keep on looking if the number next to it is not smaller)
3) The program also gets the index of the minimum number
4) We keep on going until it is in order.
// Sort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
void Sort(vector<int>&input);
int _tmain(int argc, _TCHAR* argv[]){
vector <int> input;
[Code] ....
View 4 Replies
View Related
Oct 21, 2014
I have to write a program where the user will input integer numbers. How many numbers they enter is unknown, therefor you should use a repetition structure for the input. When the user is done, they will enter -1 to exit.
Create a dynamic array if the size=2( the initial size must be 2) Repeat until user enters -1.
I have to do this without using vectors.
This is what i have, I cannot figure out what to put in main. I was thinking of a do-while?
Code:
#include <iostream>
using namespace std;
void resize(int *[], int);
int main() {
int *listDyn;
int size=2;
[code].....
View 5 Replies
View Related
Jul 14, 2014
Iam new to c++ and i want to write a program to generate an integer array. I keep getting the error at line 25: invalid types 'int[int]' for array .
int main() {
int rows;
int cols;
cin>>rows>>cols;
int test[rows][cols];
[Code] .....
View 2 Replies
View Related
Oct 9, 2013
Write a function that sorts a dictionary list alphabetically. Here's my code:
Code:
#include <stdio.h>
#include <stdbool.h>
struct entry
{
char word[15];
char definition[50];
[Code] ....
It's compiling ok, and displays the first call to the printf function from main. However, after this I get the 'Segmentation fault (core dumped)' at the terminal, presumably from the call to the dictionarySort function.
I had a little look around and found it is something to do with non-existent or not available memory addressing, I think? Even so, I can't figure out what is causing the issue in my code.
View 8 Replies
View Related
Oct 25, 2013
I am trying to assign the integer value to unsigned char array. But it is not storing the integer values. It prints the ascii values. Here the code snippet
Code: uchar uc[100];
for(i=0;i<100;i++)
{
uc[i] = i;
}
The values which are stored in uc[] is ascii values.I need the integer values to be stored in uc[]. I tried to do it with sprintf. but the output is not as expected. if I print the uc[i] it should diplay the value as 0,1,2....99.
View 9 Replies
View Related
Nov 22, 2014
This is in c. Write a program which asks for X numbers, and stores them in an array.
The program then asks the user to enter a number to look for, and tells the user how many times that number appears in the array of numbers (if any), and the array index which contain the number
Make an array of the indexes where the number was found, and then format your output to match my output.
Sample Run:
How many numbers would you like to enter: 5
Please enter a number:1
Please enter a number:2
Please enter a number:3
Please enter a number:4
Please enter a number:2
The numbers you enter were: 1, 2, 3, 4, 2
Please enter a number to find: 2
The number appears 2 times, at array ellements with indexes 1, 4
Press any key to continue . . .
View 10 Replies
View Related
Apr 16, 2013
i have a programming problem and i am unsure of what the final part is.the question is:
Write a program that fills an array with 10 random numbers between 1 and 20, displays the 10 numbers, and finds the sum of the 10 numbers. Call the getData, displayData, and getSum functions from the main function. Output the sum from the main program by calling the getSum function within a printf statement.
i am just really unsure of what this is--getData, displayData, and getSum functions from the main function. Output the sum from the main program by calling the getSum function within a printf statement.what i have got so far is;
Code:
#include <stdio.h>
#include <stdlib.h>
#define ARY_SIZE 10
int main() {
int randomNumbers[ARY_SIZE], sum = 0, i;
[Code]....
View 1 Replies
View Related
Sep 24, 2014
Write a program which stores 10 decimal numbers in an array. For these numbers accept input from the users. Once the array is populated do the following:
Display each elements of the array
Display the sum of all the elements of array
View 1 Replies
View Related
Nov 25, 2014
I have to make a program that takes several numbers into an array into a function, and send them back into the main function multiplied by 1.13. This is what I have so far:
#include <iostream>
using namespace std;
void connell(int&n[20])
main() {
int num[20];
[Code] ...
I do not know how to get numbers from an array from a main program to a function.
View 2 Replies
View Related
Jan 18, 2015
I am trying to write a program that adds numbers to an array. The array capacity starts out at 2, then doubles whenever the size reaches capacity. The program works perfectly fine when the array needs to be expanded. So I have narrowed down the issues into this one function (_dynArrSetCapacity)
Creates original array
DynArr* createDynArr(int cap) {
assert(cap > 0);
DynArr *r = (DynArr *)malloc(sizeof( DynArr));
assert(r != 0);
initDynArr(r,cap);
[Code]...
If i do createDynArr(10) for instance in the main, the program works perfectly, as it never gets expanded
View 3 Replies
View Related
Sep 24, 2014
Write a program which stores 10 decimal numbers in an array. For these numbers accept input from the users. Once the array is populated do the followings:
Display each elements of the array
Display the sum of all the elements of array
View 4 Replies
View Related
Mar 7, 2013
Create a program that display all the fibonacci numbers and display the 21st digit using array.
Here's my code:
#include <iostream>
using namespace std;
int main () {
cout<<" This program shows a series of fibonacci numbers
"<<endl;
[Code] ....
View 2 Replies
View Related
Feb 22, 2013
I am working on a program which creates a large pointer array of numbers and then performs several iterations of operations in them.
Code:
int * u = new int[N];
double * nu = new double[N];
int * nud = new int[N];
for (int i=0;i<M;i++){
for (int i=0;i<N;i++){
u[i]=0;
nu[i]=0;
[Code]...
If M is small enough then there are no problems in the program. However once M is large enough I get the "unhanded exception":
std::bad_alloc at memory location 0x0026f728..
Since I am just reusing the same arrays, and since I am able to make it through a few iterations, I didn't think it could be a memory issue. If it is, is there a way I can clear the data completely after each iteration?
View 4 Replies
View Related
Dec 1, 2014
i'm having difficulty with a problem, i need to add two big numbers suchas 54646774456776 and another one 445556777554 and it would print the result. how can i approach this problem without the use of arrays?
View 4 Replies
View Related
Oct 9, 2013
I'm supposed to write a code that takes an integer and splits it into individual numbers e.g. 234 becomes 2 3 4 .the individual numbers are then passed on to a function that accepts only one number at a time
All i could think of was this,but its a very bad method coz i dont know how long a number would be inputed into "Angle_in_degree"
Code:
int Angle_in_degree,a,b,c
c= Angle_in_degree %10;
b=Angle_in_degree/10 %10;
a=Angle_in_degree/100 % 10;
function(a);
function(b);
function(c);
View 2 Replies
View Related
Dec 6, 2014
I am writing trying to store a list of numbers into an integer as bits. So far I have this:
n=n & 0x0f
integer = integer<<2
integer = integer | n;
convert(integer));
I want it to store the integers as bits so that I am move them over and store them farther down the row as more numbers are added, but instead each new number is being added to the previous and I'm just getting a larger integer. Is this even a feasible way to store integers within an integer?
View 1 Replies
View Related
Dec 10, 2013
Alexandra has some distinct integer numbers a1,a2...an.
Count number of pairs (i,j) such that:
1≤ i ≤ n
1≤ j ≤ n
ai < aj
Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer n denoting the number of numbers Alexandra has. The second line contains n space-separated distinct integers a1, a2, ..., an denoting these numbers.
Output
For each test case, output a single line containing number of pairs for corresponding test case.
Constraints
1 ≤ T ≤ 4
1 ≤ n ≤ 100000
0 ≤ ai ≤ 109
All the ai are distinct
Example
2
2
2 1
3
3 1 2
Output:
1
3
Explanation
Case 1: Only one such pair: (2,1)
Case 2: 3 possible pairs: (2,1), (2,3), (3,1)
as I understand the problem is just counting how many Ai's are 1 <= Ai <= N and then apply ((R*(R-1))/2), R is the count of valid Ai's
My actual code is
#include <stdio.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define getNumber getchar()
#define getString getchar()
[Code] ....
View 4 Replies
View Related
Jan 6, 2015
I am supposed to convert a string made of many numbers between every number spaces are between, to an integer variable for every number..
How am I supposed to get over this problem?
Example: String: " 322 52 231"
View 1 Replies
View Related
Oct 25, 2014
The question is: Write a program that reads 3 integer numbers, then finds and prints the: Mean - Maximum & Second Minimum.
#include <iostream>
using namespace std;
int main () {
double a, b, c;
cout<< "Please enter three values"<<endl;
[Code] .....
View 6 Replies
View Related
Sep 17, 2013
Write a program asks the user for a positive integer value and then prints out all perfect numbers from 1 to that positive integer. I have been trying for some time, i found a way to check if its a perfect number or not but could not find a way to prints out all perfect numbers from 1 to that positive integer. I am here so far.
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
int n,i=1,sum=0;
cout<<"Enter a number: ";
[Code] ....
View 4 Replies
View Related
Nov 27, 2012
Why this class doesn't work for Subtraction, Division and Square Root. When I try to get results for Subtraction, it output right answer, but with any trash. And When I try to get answer for Division and Square Root, it just brakes and don't output anything. For Addition and Multiplication class work correctly.
Its the link to this class [URL]
View 2 Replies
View Related
Feb 16, 2013
I have a text file like below read.txt:
1.0 2.0 3.0 4.0
2.0 3.0 4.0 6
5.0 7 1.0 5.0
calc.cpp:
void main() {
FILE *fp;
fp=fopen("read.txt","r");
double *read_feature = new double*[3];
[Code] ....
I want to count all the numbers in my text file (read.txt). Read text file consist of floating and integer number. Answer for the above file would be integer=2 and float =10.
View 3 Replies
View Related
Jan 10, 2015
Write a program using user-defined function which accepts an integer array and its size as arguments and assign the elements into a two dimensional array of integers in the following format: If the array is 1,2,3,4,5,6, the resultant 2D array is
1 2 3 4 5 6
1 2 3 4 5 0
1 2 3 4 0 0
1 2 3 0 0 0
1 2 0 0 0 0
1 0 0 0 0 0
View 1 Replies
View Related
May 20, 2014
I want to make a program to print the product of even numbers between 1 and 30 and sum of odd numbers between 1 and 30. But the answer of product is negative. The photo shows the output of the code.
#include <stdio.h>
#include <conio.h>
void main ()
{
int i, even_product=1, odd_sum=0;
for(i=1;i<=30;i++) // For loop starts here!
[Code]...
View 5 Replies
View Related
Sep 27, 2013
I am having problem in writing the code for the problem "To assign the elements of 1-D integer array into 2-D array of integers such as if the array is 1,2,3,4,5,6 The resultant 2-D array should be like :
1 0 0 0 0 0
1 2 0 0 0 0
1 2 3 0 0 0
1 2 3 4 0 0
1 2 3 4 5 0
1 2 3 4 5 6
"
View 7 Replies
View Related