Day 1: Data Types
Day 1: Data Types
+ 0 comments Java Day 1 challenge:
I succeeded but had some trouble with the string output, putting a blank '\n' in. Resovled this by using two nextLine()s with the second one reading into my string variable. Is there a better or suggested way or is one nextLine() to eat the '\n' left over from nextDouble() standard?
+ 1 comment C# Day 2 Challenge
Its my first time here. Can someone advise why the complie woriks but addional test case fails when i submit this C# Day 2 challenge? Link: [https://www.hackerrank.com/challenges/30-data-types/problem?isFullScreen=true]
using System; using System.Collections.Generic; using System.IO; class Solution { static void Main(String[] args) { int i = 4; double d = 4.0; string s = "HackerRank "; // Declare second integer, double, and String variables. // Read and save an integer, double, and String to your variables. // Print the sum of both integer variables on a new line. // Print the sum of the double variables on a new line. // Concatenate and print the String variables on a new line // The 's' variable above should be printed first. int i2 = 12; double d2 = 4.0; string s2 = "is the best place to learn and practice coding!"; Console.WriteLine(i+i2); Console.WriteLine (String.Format("{0:0.0}",d+d2)); Console.WriteLine(String.Concat(s + s2)); } }
+ 0 comments Here is my C assignment :
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int i = 4; double d = 4.0; char s[] = "HackerRank "; int a; double b; char *c= (char*) malloc(1024*sizeof(char)); scanf("%[^\n]%*c",c); a=atoi(c); scanf("%[^\n]%*c",c); b=atof(c); scanf("%[^\n]%*c",c); c= (char*) realloc(c, strlen(c)+1); printf("%d\n",i+a); printf("%-0.1f\n",d+b); printf("%s%s",s,c); return 0; }
+ 0 comments For C users. I had some head banging up the wall because of differencies how scanf worked in my WSL environment and a given here environment. Here is a program that worked in my WSL window: // Declare second integer, double, and String variables. int my_integer = 0; double my_duoble = 0.0; char *my_string; char final_string; my_string = (char)malloc(100 * sizeof(char)); final_string = (char*) malloc(200 * sizeof(char)); if(my_string == NULL || final_string == NULL) { printf("Memory allocation failed."); return 1; }
// Read and save an integer, double, and String to your variables. scanf("%d", &my_integer); scanf("%lf", &my_duoble); scanf("%[^\n]s", my_string); // Print the sum of both integer variables on a new line. printf("%d\n", i + my_integer); // Print the sum of the double variables on a new line. printf("%.1lf\n", d + my_duoble); // Concatenate and print the String variables on a new line // The 's' variable above should be printed first. strcpy(final_string, s); strcat(final_string, my_string); printf("%s\n", final_string); free(my_string); free(final_string);
However, in the given environment nothing would be put into my_string. The remedy was to clean out a buffer from the previous input: <...>
// Read and save an integer, double, and String to your variables. scanf("%d", &my_integer); scanf("%lf", &my_duoble);int leftover = 0; while ((leftover = getchar()) != '\n' && leftover != EOF); scanf("%[^\n]s", my_string); // Print the sum of both integer variables on a new line. printf("%d\n", i + my_integer); Then my program worked. But adding a buffer cleaning code in my program in WLS, caused it crash.
Just wondering why that may be.... <...>
Then my program worked. But adding a buffer cleaning code in my program in WLS, caused it crash. Just wondering why that may be....
+ 0 comments For java: import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;
public class Solution {
public static void main(String[] args) { int i = 4; double d = 4.0; String s = "HackerRank "; Scanner scan = new Scanner(System.in); /* Declare second integer, double, and String variables. */ int j = scan.nextInt(); double e = scan.nextDouble(); scan.nextLine(); String k = scan.nextLine(); String o = s+k; /* Read and save an integer, double, and String to your variables.*/ // Note: If you have trouble reading the entire String, please go back and review the Tutorial closely. /* Print the sum of both integer variables on a new line. */ System.out.println(i+j); /* Print the sum of the double variables on a new line. */ System.out.println(d+e); /* Concatenate and print the String variables on a new line; the 's' variable above should be printed first. */ System.out.println(o); scan.close(); }
}
Sort 3853 Discussions, By:
Please Login in order to post a comment