C++ :: Using Swap Function In Array

Mar 16, 2014

The idea is to make an array and have it sort the contents inside the array in order from smallest to greatest by using a swap function. I don't know why it needs to be done this way when a sort function makes the most sense, but it is what it is.

For simplicity I want my array to only include three numbers. I was thinking {18,-2,24}. My only problem is that I am not understanding how to translate the swap function in an array. I tried using my previous swap function from another assignment and translate it to work for an array, but it doesn't work and I am completely lost and stuck. What I tried to do was this:

#include <iostream>
#include <cstdlib>
using namespace std;
const int SIZE = 3;
double myList[3] = {18, -2, 24};
void swap(myList[0], myList[1], myList[2]) {

[Code] ....

View 1 Replies


ADVERTISEMENT

C++ :: Write Swap Function To Swap 2 Elements In Vector?

Nov 15, 2013

write a swap function to swap 2 elements in the vector?

View 3 Replies View Related

C++ :: Sorting Function - Vector Swap

Jun 26, 2013

writing a sorting function that has an argument for a vector of ints rather than an array; it should use a selection sort algorithm.Here is what I have:

#include <iostream>
#include <vector>
#include <conio.h>
using namespace std;
void fillVector(vector<int> &aVector);
// PRECONDITION: number declared size of array a.
// POSTCONDITION: number_used is the number of values stored in a
//a[0] through a[number_used-1] have been filled with nonnegative int.

[code].....

View 7 Replies View Related

C++ :: Swap Function To Put String In Alphabetical Order

Nov 10, 2013

So I been working on this c++ project and I need to be able to take three seperate strings and send them to function to put them in alphabetical order through a-z and use the swap function to return them in order. I been searching for problems like this but I haven't fame across any. I can copy my code onto here as well as a more detailed description of what I'm needing to do onto here if needed.

View 4 Replies View Related

C++ :: Thread With Lambda Function - Swap 3 Variables

May 13, 2014

I am trying to realize a simple code with thread and lambda function.

My goal is to swap 2 variable . I launch 2 thread,

The first:
Put his value in a shared variable and notify it .
wait until an event on a condition variable occur.
read from shared value .

The second wait until an event on a condition variable occur.
Wake up
read from shared value .
Put his value in a shared variable and notify it.

This is the code

thread t1([&p1]()->void{
m.lock();
nt++;
if(nt==1) {
//first thread
unique_lock<mutex> u1(m); ***

[Code] .....

Why it say error "abort() has been called " on the istr ***

View 2 Replies View Related

C/C++ :: Swap A Digit From A Number With Another Digit Using Function

Oct 26, 2014

Write a function which will take 3 arguments. The function needs to return a new number which is formed by replacing the digit on a given position in the number with a digit which is carried as an argument (the position in the number is counted from right to left, starting with one). Write a main program which will print the newly formed number.

Examples:
A function call of 2376, 3 and 5 should return the number 2576
A function call of 123456, 4 and 9 should return the number 129456

What I succeeded to do so far:
Figure out the logic for swapping the digit and write working code for it (in the main function).

What I failed to do so far:
Write a function which will return the desired result.

What is my problem:
I tried writing a function to do this, but as you see from my calculations, my result is divided in 3 parts. I don't know how to return more variables from a function.

Code:

#include <stdio.h>
int main() {
int inputNumber, swapPosition, swapDigit;
scanf("%d%d%d", &inputNumber, &swapPosition, &swapDigit);
int i, numberPart1 = inputNumber;
for (i = 1; i <= swapPosition; i++)

[Code] ...

View 8 Replies View Related

C++ :: Swap Between Two References?

May 28, 2013

I have the following code segment:

Code:

void Swap(Number& num1, Number& num2)
{
cout<<"Before swap:"<<num1<<" "<<num2<<endl;
Number& temp=num1;
num1=num2;
num2=temp;
cout<<"After swap:"<<num1<<" "<<num2<<endl;
}

[code]...

to which the output is:

Code:

Before swap:13 11

After swap:13 11

13 11 that seems confusing.

why doesn't Swap() swap the two Numbers?

View 8 Replies View Related

C++ :: Swap Is Not Declared?

Jun 15, 2013

#include <iostream>
#include <conio.h>
using namespace std;

[Code]....

appears that error on line 25 & 30 where swap1 & swap2 is not declared in this scope.

View 6 Replies View Related

C :: Swap Chars Using Getchar

Sep 23, 2014

This is for homework . Must use only getchar and putchar

Code:

int main(void) {
int pch; //first
int ch; //second

[Code]....

And it works , but i need to hit ENTER two times when i have 3,5,7... chars to print result.

View 6 Replies View Related

C++ :: Swap Elements Of Vector

Apr 21, 2014

how to swap the first and 'mid' elements of a vector?

View 2 Replies View Related

C/C++ :: Swap Value Of Two Rows In A Matrix Among Themselves

Mar 21, 2015

I want to swap the value of two rows in matrix among themselves, the index of rows are user defined.

void swapRows(int matrix[M][N], int m, int n, unsigned short R1, unsigned short R2) {
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
//dunno what to put in here

[Code] .....

View 6 Replies View Related

C++ :: Swap Two Strings Without Using 3rd Variable?

Jan 8, 2013

How to swap two strings without using 3rd variable? could it be done using constructors? If yes how?

View 11 Replies View Related

C++ :: Swap Values Of Integer And A Character

Oct 31, 2013

Im trying to swap the values of an integer and a character, however Im not sure where to insert the static_cast<type> part that I need for this to happen?

// Program to demonstrate a function template
#include <iostream>
using namespace std;
// Interchanges the values of variable1 and variable2
template<class T>
void swap_values(T& variable1, T& variable2)

[Code] ....

View 5 Replies View Related

C/C++ :: How To Swap First And Last Letters Of Input Stream

Sep 22, 2014

I simply need to know how I could swap the first and last letters of the input in this program:

int main() {
    cout << "---------------------------------------------------------" << endl;
    cout << "              Letter Swapping Program" << endl;
    cout << "---------------------------------------------------------" << endl;  
    string word;
    cout << "Please enter a word at least 3 letters long: ";
    cin >> word;

[Code] ....

By all means, I know this is a messy program and is not the most concise way to write it....

View 1 Replies View Related

C :: Local Variables - Swap Char Pointers

Apr 23, 2013

I have the following code. According to this the values of pointers p[0] and p[1] remains unchanged since the swap is made to local variables in swap function.Now my doubt is how can I swap the pointers p[0] and p[1] inside the function swap??

Code:

#include<stdio.h>int main(){char*p[2]={"hello","good morning"};
swap(p[0],p[1]);
printf("%s %s",p[0],p[1]);return0;
}void swap(char*a,char*b){char*t; t=a; a=b; b=t;
}

View 5 Replies View Related

C++ :: Program To Swap Positions Of Digits Of User

Oct 11, 2014

I'm using code blocks ....

1.Write a program to swap positions of digits of a user entered three-digit integer N, where N is equal or between 101 and 999. (i.e. if user enters 389 your program should print 983. If user enters 300 program should print 003). Repeatedly ask user for correct N, if he/she enters an integer N which is not in the range.

2. Given that y= 4*( 1- 1/3 + 1/5- 1/7+ 1/9-...plus or minus 1/N) Write a program using a for-loop or a while-loop to compute and print the sum of first 50 terms of y.

3. a) Write a user-defined function funGx to compute G(x), where

5 if x<-10

x^2 +(5/x) if -10 <=x<-5

x^2 - (5/x-5) if -5<=x<5

x^2 -(5/x) if 5<=x<10

-5 if x>=10

b) Call the user-defined function funGx in main function to compute and print G(x)values for x= -15.5 , x=5, and x= 0.5 in an informative sentence.

View 3 Replies View Related

C/C++ :: Using Linked Lists To Swap Two Adjacent Elements

Jan 6, 2015

I am reading a book currently on data structures in c++. The questions I have is how I would be able to swap two adjacent elements by adjusting only the links (not the data) using, a) singly linked lists, doubly linked lists.

For the single linked list which I am somewhat familiar with (by the content of the book), I would consider taking the Node A, and copying its data into a new Node temp, then re-routing the pointer from whatever connected to Node A, now to Node temp. now I want to re-route the pointer of Node B to Node temp and Node temp to whatever Node was being connected from Node B. Is this the correct approach?

View 1 Replies View Related

C/C++ :: Invalid Lvalue In Assignment While Trying To Swap Pointers

Apr 1, 2013

While executing this code i was getting a error Invalid lvalue in assignment. Can any one tell how to correct this.

dataItem* d=(dataItam*)malloc(10*sizeof(dataItem));
dataItem* temp;
temp=(d+6);
(d+6)=(d+8);//error line
(d+8)=temp;//error line

View 3 Replies View Related

C/C++ :: Game Of Fifteen - Swap Move Not Showing On Screen

May 26, 2014

I am having problems with my game of fifteen. I have implemented the swap> I know the swap takes place[using GDB] but the swap does not show on the screen even if GDB says it has taken place. I am getting no errors from the move function so I know that something is taking place. I was thinking that the problem may be in the draw function but it looks okay to me. I have looked at this over and over but I don't know why the draw is not printing the move to the screen.

#define _XOPEN_SOURCE 500
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
// board's minimal dimension
#define MIN 3
// board's maximal dimension
#define MAX 9

[Code] ....

View 14 Replies View Related

C++ :: Swap Adjacent Nodes In Doubly Linked List?

Feb 28, 2013

I have been trying to swap two adjacent nodes for my linked list sort. Its not meant to be optimal, just meant to work. My problem is I either lose nodes or get Access Violation errors.

PHP Code:

void List::sortList() {
    Node *current = _head;
    Node *rightNode = NULL;        
    if (_head->_data > _head->_next->_data) {            
        current = _head;
        rightNode = _head->_next;

[Code] .....

View 10 Replies View Related

C++ :: Passing 2 Dimensional Array Through Median Function To One Of 2 Other Function

Aug 20, 2013

I want to use one median function "selectfunction" to choose one of the 2 other functions at random to pass my 2-dim array to the selected function. There is a problem in the median function

#include <iostream>
#define random(x)(rand()%x) // for random number between numbers of 0 and 1
using namespace std;
void proc1 (int iArray[][2]);
void proc2 (int iArray[][2]);
void selectfunction(int iArray[][2]);
int A[4][2] = {{1, 2} , {3, 4} , { 5, 7} , {8, 1} };

[Code]...

View 1 Replies View Related

C++ :: Write Function That Takes Array And Returns True If All Elements In Array Are Positive

Jan 21, 2013

Write a function that takes an array and returns true if all the elements in the array are positive, otherwise, it returns false.

View 6 Replies View Related

C :: How To Make A Function Read From Array A But Put Select Parts In Array B

Jul 30, 2013

im trying to read in 1 array and get 2 as outputs from 3 different functions.my read array is easy enough were im getting confused is how to read that array, separate it and take out only the parts i want and place them into a 2nd, then again a 3rd array.i have the following so far:

Code:

#include<stdlib.h>#include<stdio.h>
#include<unistd.h>
#define SIZE 10
}

[code]....

this compiles without a complaint, but when i go to run it no longer responds after taking the 10th element (well 9th if counting from 0).I think i have the if correct for the even odd section, but when i try to populate B or C array with the output of that if statement from A is were i think things are dying...

View 12 Replies View Related

C :: Print Values Of Array From Function By Passing Array?

Nov 1, 2014

I wanted to print the values of a array from a function by passing the array as well as the number of elements to be read. For a single dimensional array, this is how i have written it. It's pretty straight forward. I want to read 5 elements from the 5th element in the array.

Code:
#include<stdio.h>
void display(int array[],int size) {
int i;

[Code]....

With this code I want to print the five elements from the element present in [0][4].

But shows an error that

Code:
D:BennetCodeblocks CLearning CSingleDimentionalArray.c||In function 'main':|
D:BennetCodeblocks CLearning CSingleDimentionalArray.c|18|warning: passing argument 1 of 'display' from incompatible pointer type [enabled by default]|
D:BennetCodeblocks CLearning CSingleDimentionalArray.c|2|note: expected 'int (*)[10]' but argument is of type 'int *'|
||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

I know when you pass a array as an argument it gets decomposed into a pointer, but with a multi-dimensional array this is not the case. how this works for mult- dimensional array's?

View 3 Replies View Related

C++ :: Passing 2D Array To A Function Where Size Of Array Is Not Known At Runtime

Jun 27, 2014

I'm wondering if it is possible to pass a 2d array to a function where the size of the array is not known at runtime.

I've tried

function ( array[][6] ) ;

But the size of the array has to be constant so it cannot be declared later.

I've tried using a template but you still have to declare the size of the array at runtime. Is this even possible at all?

The only other way I can think of is using a dynamic 2d array but how to create one and manipulate it.

View 2 Replies View Related

C :: How To Use 2D Array In Function And Change Array Values

Apr 17, 2014

use 2D array in function and change the array values. I do not know if I can use array by calling from a function. I have 6 row 6 column array, I used it inside a function and for the another function I just need to change 4. row 4. column and I do not want to type array to just change one part. I do not know if there is another way or not

View 7 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved