0% found this document useful (0 votes)
14 views6 pages

28

The document contains various C++ programming examples demonstrating the use of arrays, functions, and basic operations such as initialization, counting elements, and finding minimum values. It also discusses the memset function for initializing array values to a specific number. Additionally, it includes implementations of a swap function and methods for printing and modifying array values.

Uploaded by

loveumusic31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

28

The document contains various C++ programming examples demonstrating the use of arrays, functions, and basic operations such as initialization, counting elements, and finding minimum values. It also discusses the memset function for initializing array values to a specific number. Additionally, it includes implementations of a swap function and methods for printing and modifying array values.

Uploaded by

loveumusic31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1) Find out what is the use of memset function.

2) int arr[10] = {0}; -> All the values are initialized to 0


int arr[10] = {1}; -> All values won't be initialized to 1. Find using
memset how to initialize with 1 or any particular value other than 0.
3) Implement swap function on your own way. (3 ways) --> Find all.
//array
#include<iostream>
using namespace std;
int main(){
int arr[7]; // declaration of integer array of 28 bytes space
int a[53]; // integer array of 53 size
char ch[106]; // character array of 106 size
bool arr1[10]; // boolean array of 10 size
int arr2[] = {1,2,3,4,5}; // initialization of array
// Printing array values
int array[5] = {1,2,3,4,5};
for(int i = 0;i<5;i++){ // i is the index
cout<<array[i]<<" ";
}
// Taking the input and initializing the array
int array1[10];
cout<<"Enter array values "<<endl;
for(int i = 0;i<10;i++){
cin>>array1[i];
}

return 0;
}
//array and functions
#include<iostream>
using namespace std;
// Here original array is passed by default
void incr(int arr[],int size){ // good practice to pass size along with
the array
arr[0] = arr[0] + 10;
}
int main(){
int arr[5] = {1,2,3,4,5};
cout<<arr[0]<<endl; // 1 is printed
incr(arr,5);
cout<<arr[0]; // Actual array is updated (1+10 = 11 is printed)
return 0;
}
//change array values to 1
#include<iostream>
using namespace std;
int main(){
int arr[5] = {1,2,3,4,5};
for(int i = 0;i<5;i++){
// Changing value to 1
arr[i] = 1;
}
for(int i = 0;i<5;i++){
// Just print the array. We get changed values
cout<<arr[i]<<" ";
}
return 0;
}
//count 0 and 1 in array
#include<iostream>
using namespace std;
int zeroArr(int arr[],int size){
int cnt = 0;
for(int i = 0;i<size;i++){
if(arr[i] == 0) // If current element is 0, then increase count
cnt++;
}
return cnt;
}
int oneArr(int arr[],int size){
int cnt = 0;
for(int i = 0;i<size;i++){
if(arr[i] == 1) // If current element is 1, then increase count
cnt++;
}
return cnt;
}
int main(){
int arr[10] = {1,1,0,1,0,0,1,0,1,1};
int size = 10;
int zeroCount = zeroArr(arr,size);
int oneCount = oneArr(arr,size);
cout<<"Number of zeroes are "<<zeroCount<<endl;
cout<<"Number of ones are "<<oneCount<<endl;
return 0;
}
//extreme points in array
#include<iostream>
using namespace std;
int main(){
int arr[6] = {1,2,3,4,5,6};
int start = 0;
int end = 5;
while(start<=end){
if(start == end){ // for odd case
cout<<arr[start]<<" "; // print once only
break;
}
cout<<arr[start]<<" "; // normal case printing
cout<<arr[end]<<" "; // normal case printing
start++;// updating start pointer/variable
end--; // updating end pointer/variable
}
return 0;
}
//min in arr
#include<iostream>
#include<limits.h>
using namespace std;
int main(){
int arr[5] = {3,1,2,5,4};
int mini = INT_MAX; // best practice is to make mini = maximum
possible integer value
for(int i = 0;i<5;i++){ // linear search
if(arr[i]<mini){ // comparing current number with the current
minimum
mini = arr[i]; // update mini if condition is fulfilled
}
}
cout<<"Maximum element = "<<mini; // printing the minimum
value
return 0;
}
//print doubles of values in array
#include<iostream>
using namespace std;
int main(){

int arr[5] = {1,2,3,4,5};


for(int i = 0;i<5;i++){
// Just multiply by 2 and print
cout<<2*arr[i]<<" ";
}
return 0;
}

You might also like