Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,913 questions

51,846 answers

573 users

How to swap elements in int array with C++

1 Answer

0 votes
#include <iostream>

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) { 
	    std::cout << arr[i] << " ";
    }
}
 
int main () {
    int arr[] = { 1, 2, 3, 100, 5, 6, 7 };  
    int size = sizeof(arr) / sizeof(arr[0]);

    printArray(arr, size);
    std::cout << "\n";
                                                
    std::iter_swap(arr + 1, arr + 3); 
     
    printArray(arr, size);                                           
}
 
 
 
 
/*
run:
 
1 2 3 100 5 6 7 
1 100 3 2 5 6 7 
 
*/

 



answered Nov 22, 2022 by avibootz

Related questions

1 answer 87 views
1 answer 154 views
1 answer 129 views
1 answer 155 views
2 answers 171 views
171 views asked Jan 14, 2021 by avibootz
1 answer 92 views
92 views asked Nov 22, 2022 by avibootz
1 answer 219 views
219 views asked Aug 1, 2020 by avibootz
...