I have problem swap variables function in Dart - algorithm

I'm gonna crazy everything is looking good where is the problem?
void main(){
int num1 = 10;
int num2 = 11;
print("First : $num1, $num2");
swap(num1,num2);
print("Last : $num1, $num2");
}
void swap(int num1,int num2){
int temp;
temp = num1;
num1 = num2;
num2 = temp;
}
MY OUTPUT:
First: 10,11
Last: 10,11

You should try removing the swapping from the swap function and place it directly inside the main function. When calling functions, references to the variables' values are passed and not the variable itself.

#Ketan Ramteke is right. Or you can return a list and use that value like this:
void main(){
int num1 = 10;
int num2 = 11;
print("First : $num1, $num2");
List arr = swap(num1,num2);
print("Last : ${arr[0]}, ${arr[1]}");
}
List swap(int num1,int num2){
int temp;
temp = num1;
num1 = num2;
num2 = temp;
return [num1, num2];
}

// Dart 2.6.
void main(){
int num1 = 10;
int num2 = 11;
print("First : $num1, $num2");
// get returned swapped values
List result = swap(num1,num2);
// assign them to original variables
num1 = result[0];
num2 = result[1];
//print the mutated num1 and num2
print("Last : $num1, $num2");
}
List swap(int num1,int num2){
int temp;
temp = num1;
num1 = num2;
num2 = temp;
// print("Last : $num1, $num2");
return [num1, num2];
}

Related

For Loop Iteration and Algorithm Can Someone help me?

Can Someone help me?
I want to Iterate the Value of 'n' in This Manner. in any Language.
please explain the Concept.
if n = 5
sum = (5x5)+(4x4)-(3x3)+(2x2)-(1x1)
sum = 35;
I want to display like that value.
but it should iterate in this Manner
(nxn)+(n-1xn-1)-(n-2xn-2)+(n-3xn-3)-(n-4xn-4)+...;
When n-x==1 that time the iteration should get stop and Value should be printed.
for(i=1;i<=n;i+){
sum=0
for(j=1;j<=i;j++){
sum = sum+(j*j);
if(j<=i){
print(sum);
}
}
}
This Is the Answer which i got.
#include <iostream>
using namespace std;
int main()
{
int limit;
int sumOfSquare = 0;
cin>>limit;
sumOfSquare= limit*limit;
for(int num = 1; num<limit; num++)
{
if(limit%2==0){
if(num%2==0)
sumOfSquare = sumOfSquare - (num * num);
else
sumOfSquare = sumOfSquare + (num * num);
}else{
if(num%2==0)
sumOfSquare = sumOfSquare + (num * num);
else
sumOfSquare = sumOfSquare - (num * num);
}
}
cout<<"Output of " << limit << " is " << sumOfSquare;
return 0;
}

Array by value multiplication memory leak in c++

I'm having some trouble with multiplying an array (char array in this particular case) by a value.
My code looks like this:
char* tab1 = copy("11");
char t = '2';
int length = strlen(tab1) + 2;
char*result = populate('0', length);
int p_length = strlen(tab1);
for (int j = p_length - 1; j >= 0; j--) {
char* tmp = multiply_chars(tab1[j], t);
v_shove(tmp, j);
char* tmp2 = add_tables(result, tmp);
delete[] result;
result = tmp2;
delete[] tmp;
}
cout << result << endl;
delete[] result;
delete[] tab1;
None of the methods used (that's populate, multiply_chars and add_tables) causes a leak when ran in an infinite loop. I've narrowed the leak to the
char* tmp2 = add_tables(result, tmp);
delete[] result;
result = tmp2;
part, but have no idea why it would happen.
I check for leaks by running snippets in an infinite loop and checking memory usage.
Any help would be appreciated! If need be I'll post the code of the methods used, but decided not to for the sake of brevity here. They all return new cstrings. Also, the t2 variable is there from when I was checking the array by array multiplication, which also leaked - decided to do array by value multiplication first.
(Now, to be completely honest this is one of the methods required for a school project, but it's such a miniscule part of it, that I thought it wouldn't hurt if I asked - the teacher isn't really big on helping with particular code problems)
The functions are:
char * add_tables(const char * table1, const char * table2)
{
char* tmp1 = get_string_trailing("0",table1);
char* tmp2 = get_string_trailing("0", table2);
int l1 = strlen(tmp1), l2 = strlen(tmp2);
if (l1 != l2) {
if (l1 > l2) {
char* t = resize_string(tmp2, l1 - l2, '0');
delete[] tmp2;
tmp2 = t;
}
else {
char* t = resize_string(tmp1, l2 - l1, '0');
delete[] tmp1;
tmp1 = t;
}
}
int length = strlen(tmp1) + 2;
char*result = new char[length];
result[length - 1] = 0;
int buffer = 0;
for (int i = length - 2; i > 0; i--) {
int t = buffer + (tmp1[i-1]-'0') + (tmp2[i-1]-'0');
result[i] = (t% 10)+'0';
buffer = (t - (t % 10))/10;
}
result[0] = buffer + '0';
char* t = get_string_trailing("0", result);
delete[]result;
result = t;
delete[]tmp1;
delete[]tmp2;
return result;
}
void v_shove(char *&c, int i)
{
char* tmp = shove(c, i);
delete[] c;
c = tmp;
}
char * populate(const char populator, int length)
{
char* result = new char[length + 1];
result[length] = 0;
for (int i = 0; i < length; i++) {
result[i] = populator;
}
return result;
}
char * multiply_chars(const char c1,const char c2)
{
char*result = new char[3];
result[2] = 0;
char tmp1 = c1 - '0', tmp2 = c2 - '0';
result[1] = (tmp1*tmp2 % 10) + '0';
result[0] = (tmp1*tmp2 - (tmp1*tmp2 % 10)) / 10 + '0';
char* r = get_string_trailing("0", result);
delete[] result;
result = r;
return result;
}
int get_length_trailing(const char * ignore,const char * table)
{
int length = 0;
int i = 0;
bool flag = true;
while (i < strlen(table)) {
if (flag) {
for (int j = 0; j < strlen(ignore); j++)
if (table[i] == ignore[j])goto BREAKPOINT;
}
flag = false;
length++;
BREAKPOINT:i++;
}
return length;
}
char * get_string_trailing(const char * ignore,const char * table)
{
int result_length = get_length_trailing(ignore, table);
char* result = new char[result_length + 1];
int counter = 0;
int i = 0;
bool flag = true;
while (i < strlen(table)) {
if (flag)
for (int j = 0; j < strlen(ignore); j++)
if (table[i] == ignore[j])goto BREAKPOINT;
flag = false;
result[counter] = table[i];
counter++;
BREAKPOINT:i++;
}
result[result_length] = 0;
if (result_length == 0) return copy("0");
return result;
}
char * shove(const char * table1, int index)
{
char*result = "0";
int length = strlen(table1) + index + 1;
result = new char[length];
result[length - 1] = 0;
if (index > 0) {
for (int i = 0; i < strlen(table1); i++)
result[i] = table1[i];
for (int i = 0; i < index; i++)
result[strlen(table1) + i] = '0';
}
else {
for (int i = 0; i < strlen(result); i++)
result[i] = table1[i];
}
char* t = get_string_trailing("0", result);
delete[] result;
result = t;
return result;
}
There is at least a memory leak in get_string_trailing: if result_length is zero, you return a copy and do not delete result. There are also confusions between "string" (such as "0") and 'char': with double quotes, the terminating string character (\0) is automatically appended to the string, while simple quotes only define a character. So "0" is made of 2 char in memory and can not be stored in a pointer (undefined behavior, overwriting memory).
To summarize: here you are writing C, not learning C++. If you have to deal with C strings (you are writing a low-level pilot in C or your professor still doesn't understand that C and C++ are different languages), at least use the functions of the string.h (in C) / cstring (in C++) header to minimize the chance of memory leak or undefined behavior. If you do not have to use C strings, use std::string and the string manipulation tools of the standard library. Your work will be much easier, and your code much less vulnerable to bugs:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string tab1("11")
string t("2") // never use the single quotes for a string
cout << stoi(tab1) * stoi(t) << endl;
return;
}
That's it!

Duplicate Local Variable error in a For loop

I'm trying to make a program that finds the sum of all the integers between two numbers, inclusive. I'll paste what I have so far, but I'm getting an error (i is a duplicate local variable?). Thanks
public int sum(int num1, int num2){
if (num1 == num2){
return num1;
}
if (num1 > num2){
for (int i = (num2 + 1), i <= num1, i++){
num2 += i;
}
return num2;
}
if (num2 > num1){
for(int i = num1 + 1, i <= num2, i++){
num1 += i;
}
return num1;
}
}
Use semi-colons in your for loops, not commas:
public int sum(int num1, int num2){
if (num1 == num2){
return num1;
}
if (num1 > num2){
for (int i = (num2 + 1); i <= num1; i++){
num2 += i;
}
return num2;
}
if (num2 > num1){
for(int i = num1 + 1; i <= num2; i++){
num1 += i;
}
return num1;
}
}

c++ - how to convert char to int?

I try to write a function who convert a string to an integer (like atoi). I don't see why my function "convertir" don't print my variable "res " whereas "test 1 " "test 2"... "test 4" is printed. I let you look at my code and if you see something bad tell me please.
#include "stdio.h"
#include "stdlib.h"
int xpown(int x, int n); // x^n
int lent(char str[]); // return length of string
int convertir(char s[]); //convert char to int
int main(){
char s[] ="1234";
convertir(s);
return 0;
}
int xpown(int x, int n){
int res = 1;
while (n != 1){
res= res*x;
n--;
}
return res;
}
int lent(char str[]){
int res =0;
int i=0;
while (str[i] != '\0'){
res=res+1;
i++;
}
return res;
}
int convertir(char s[]){
int res = 0;
int i = lent(s);
int j = 0;
char c = s[j];
while (c != '\0'){
c=s[j];
printf("test %d \n", j);
res = res + (c - 48) * xpown(10,i);
i--;
j++;
}
printf("%d", res);
}
You're setting i too high. Consider the simplest case, where s has 1 digit. You want to multiply that digit by 1 (100), not 10 (101). So it should be:
int i = lent(s) - 1;
BTW, you shouldn't hard code the value 48, use '0':
res += (c - '0') * xpown(10, i);
The standard function atoi() will likely do what you want.

Run length encoding using O(1) space

Can we do the run-length encoding in place(assuming the input array is very large)
We can do for the cases such as AAAABBBBCCCCDDDD
A4B4C4D4
But how to do it for the case such as ABCDEFG?
where the output would be A1B1C1D1E1F1G1
My first thought was to start encoding from the end, so we will use the free space (if any), after that we can shift the encoded array to the start. A problem with this approach is that it will not work for AAAAB, because there is no free space (it's not needed for A4B1) and we will try to write AAAAB1 on the first iteration.
Below is corrected solution:
(let's assume the sequence is AAABBC)
encode all groups with two or more elements and leave the rest unchanged (this will not increase length of the array) -> A3_B2C
shift everything right eliminating empty spaces after first step -> _A3B2C
encode the array from the start (reusing the already encoded groups of course) -> A3B2C1
Every step is O(n) and as far as I can see only constant additional memory is needed.
Limitations:
Digits are not supported, but that anyway would create problems with decoding as Petar Petrov mentioned.
We need some kind of "empty" character, but this can be worked around by adding zeros: A03 instead of A3_
C++ solution O(n) time O(1) space
string runLengthEncode(string str)
{
int len = str.length();
int j=0,k=0,cnt=0;
for(int i=0;i<len;i++)
{
j=i;
cnt=1;
while(i<len-1 && str[i]==str[i+1])
{
i++;
cnt++;
}
str[k++]=str[j];
string temp =to_string(cnt);
for(auto m:temp)
str[k++] = m;
}
str.resize(k);
return str;
}
null is used to indicate which items are empty and will be ignored for encoding. Also you can't encode digits (AAA2222 => A324 => 324 times 'A', but it's A3;24). Your question opens more questions.
Here's a "solution" in C#
public static void Encode(string[] input)
{
var writeIndex = 0;
var i = 0;
while (i < input.Length)
{
var symbol = input[i];
if (symbol == null)
{
break;
}
var nextIndex = i + 1;
var offset = 0;
var count = CountSymbol(input, symbol, nextIndex) + 1;
if (count == 1)
{
ShiftRight(input, nextIndex);
offset++;
}
input[writeIndex++] = symbol;
input[writeIndex++] = count.ToString();
i += count + offset;
}
Array.Clear(input, writeIndex, input.Length - writeIndex);
}
private static void ShiftRight(string[] input, int nextIndex)
{
var count = CountSymbol(input, null, nextIndex, (a, b) => a != b);
Array.Copy(input, nextIndex, input, nextIndex + 1, count);
}
private static int CountSymbol(string[] input, string symbol, int nextIndex)
{
return CountSymbol(input, symbol, nextIndex, (a, b) => a == b);
}
private static int CountSymbol(string[] input, string symbol, int nextIndex, Func<string, string, bool> cmp)
{
var count = 0;
var i = nextIndex;
while (i < input.Length && cmp(input[i], symbol))
{
count++;
i++;
}
return count;
}
The 1st solution does not take care of single characters. For example - 'Hi!' will not work. I've used totally different approach, used 'insert()' functions to add inplace. This take care of everything, whether the total 'same' character is > 10 or >100 or = 1.
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
string name = "Hello Buddy!!";
int start = 0;
char distinct = name[0];
for(int i=1;i<name.length()+1;){
if(distinct!=name[i]){
string s = to_string(i-start);
name.insert(start+1,s);
name.erase(name.begin() + start + 1 + s.length(),name.begin() + s.length() + i);
i=start+s.length()+1;
start=i;
distinct=name[start];
continue;
}
i++;
}
cout<<name;
}
Suggest me if you find anything incorrect.
O(n), in-place RLE, I couldn't think better than this. It will not place a number, if chars occurence is just 1. Will also place a9a2, if the character comes 11 times.
void RLE(char *str) {
int len = strlen(str);
int count = 1, j = 0;
for (int i = 0; i < len; i++){
if (str[i] == str[i + 1])
count++;
else {
int times = count / 9;
int rem = count % 9;
for (int k = 0; k < times; k++) {
str[j++] = str[i];
_itoa(9, &str[j++], 10);
count = count - 9;
}
if (count > 1) {
str[j++] = str[i];
_itoa(rem, &str[j++], 10);
count = 1;
}
else
str[j++] = str[i];
}
}
cout << str;
}
I/P => aaabcdeeeefghijklaaaaa
O/P => a3bcde4fghijkla5
Inplace solution using c++ ( assumes length of encoding string is not more than actual string length):
#include <bits/stdc++.h>
#include<stdlib.h>
using namespace std;
void replacePattern(char *str)
{
int len = strlen(str);
if (len == 0)
return;
int i = 1, j = 1;
int count;
// for each character
while (str[j])
{
count = 1;
while (str[j] == str[j-1])
{
j = j + 1;
count++;
}
while(count > 0) {
int rem = count%10;
str[i++] = to_string(rem)[0];
count = count/10;
}
// copy character at current position j
// to position i and increment i and j
if (str[j])
str[i++] = str[j++];
}
// add a null character to terminate string
if(str[len-1] != str[len-2]) {
str[i] = '1';
i++;
}
str[i] = '\0';
}
// Driver code
int main()
{
char str[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabccccc";
replacePattern(str);
cout << str;
return 0;
}

Resources