Why this code doesn't print the full array.How to correct the code or improve to print the full array
int main(void) {
float value[MAX], a;
int bit, i;
int group[10];
bit = 0;
do {
scanf("%f", &a);
value[bit] = a;
bit++;
} while (a == '\n');
for (i = 0; i < bit; i++)
printf("%f", value[i]);
}
Change the stop condition of your reading from
while(a=='\n');
to some int/float value.
As mentioned by someprogrammerdude, variable "a" never be equal to '\n'.
Related
I've only used C++ in the past and I was trying to convert my decimal to hex code to C. When trying to do so, I'm noticing when I try to print out a character, it prints out the ASCII value instead. I'm unsure of why it is doing this. Example: 10 in hex = A, however it prints out 65 instead. Any help would be appreciated.
#include<stdio.h>
#include<stdlib.h>
int main ()
{
int num = 0;
printf("Please enter an integer ");
scanf("%d",&num);
//shown as 8 in the example
char hex[8];
char hex_values[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int count = 0;
while(num > 0)
{
int spot = num % 16;
hex[count] = hex_values[spot];
num = num / 16;
count++;
printf("%d ", hex[count-1]);
}
//places zeros in front of the array... in an easy way
int zeros = 8 - count;
for(int q = 0; q < zeros; q++)
{
printf("%c", '0');
}
}
You have to print with '%c' to get the ASCII representation of a number because in C char are just unsigned short int
printf("%c ", hex[count-1]);
I was trying to make a program where if I enter an integer, the program would find out the bigger number and subtract it by the smaller number. This part, I got it.
The problem is, the infinite loop part.
I tried to get type in two integers keep on printing with the while loop, and break when at least one character is typed in.
For example, if I type in 2 #, it would break.
But I couldn't find the write place to get the break; within the code and therefore whenever I enter a character it would keep on creating an infinite loop.
Is there any way to create a break in this code? I humbly ask for advice...
The following is the code which I couldn't put the break
(By the way, the reason I did the condition in while as sizeof(i)==4 || sizeof(j)==4 was to make it so it would only enter an integer, since the size of an integer is 4)
int main()
{
int i, j;
int result;
while (sizeof(i)==4 || sizeof(j)==4){
printf("type in two integers : ");
scanf("%d %d", &i, &j);
if (i < j) {
result = j - i;
}
else if (j < i){
result = i - j;
}
printf("%d\n", result);
}
return 0;
}
The bottom code is the one I tried to put break but failed (it kept creating an infinite loop)...
int main()
{
int i, j;
int result;
while (sizeof(i)==4 || sizeof(j)==4){
if (sizeof(i) == 4 || sizeof(j) == 4) {
printf("type in two integers : ");
scanf("%d %d", &i, &j);
if (i < j) {
result = j - i;
}
else if (j < i) {
result = i - j;
}
printf("%d\n", result);
}
else
break;
}
return 0;
}
and here's a code where I got rid of the sizeof and used while(1), though there wasn't much change in the fact that the break didn't work...
int main()
{
int i, j;
int result;
while (1){
printf("type in two integers : ");
scanf("%d %d", &i, &j);
if (i < j) {
result = j - i;
}
else if (j < i) {
result = i - j;
}
printf("%d\n", result);
}
return 0;
}
You can't use sizeof(i) to do run-time checks! This is a compile-time constant that, in your case (32-bit integers) will always evaluate to 4.
In order to check that two valid integers have been given, you can check the return value of the scanf function (it gives the number of fields successfully scanned):
#include <stdio.h>
int main()
{
int i, j;
int result;
while (1) {
printf("type in two integers : ");
if (scanf("%d %d", &i, &j) != 2) break; // Break here if we didn't get two integers
if (i < j) {
result = j - i;
}
else if (j < i) {
result = i - j;
}
printf("%d\n", result);
}
return 0;
}
Feel free to ask fir further clarification and/or explanation.
Drop the whole concept of endless loop with break inside if.
Make a condition for the loop based on the return value of scanf(), that is practically what it is designed for.
#include <stdio.h>
int main()
{
/* always init everything */
int i=0, j=0;
int result=0;
printf("type in two integers : ");
while (2==scanf("%d %d", &i, &j))
{
if (i < j) {
result = j - i;
}
else /* removed second if, to have a meaningful result for i==j */
{
result = i - j;
}
printf("%d\n", result);
printf("type in two integers : ");
}
return 0;
}
I'd probably actually use do {...} while (...) with a variable storing the return value of scanf()for being used in the loop condition. I'd consider it more elegant for not having to copy the print, but I kept it closer to your code structure.
More comments on your code:
as explained in comments, sizeof() works differently than you seem to think; it is static and does not change at runtime and hence cannot be used in a loop condition
with while (sizeof(i)==4 || sizeof(j)==4){if (sizeof(i) == 4 || sizeof(j) == 4){/* a */} else {/* b */}, b cannot ever be reached, because the conditions of while and if are identical
check the possible outcomes of the if conditions inside the loop, you are leaving the one with i==j undefined and return an uninitialised value
always init all variables as a habit
for a good MRE include the include lines
On your request, here is a proposal for the do-while alternative:
#include <stdio.h>
int main()
{
/* always init everything */
int i=0, j=0;
int result=0;
int iScanned=0;
do
{
printf("type in two integers : ");
iScanned=scanf("%d %d", &i, &j); /* keep the return value for loop */
if (i < j) {
result = j - i;
}
else /* removed second if, to have a meaningful result for i==j */
{
result = i - j;
}
if(2==iScanned) printf("%d\n", result); /* if to avoid awkward last output */
} while (2==iScanned);
return 0;
}
I have this simple code that inserts the number entered in the terminal at the position of the vector at each loop.
int main()
{
int vector[5];
int i;
for (i = 0; i < 5; i++)
{
printf("Number: ");
scanf_s("%d", &vector[i]);
}
system("pause");
return 0;
}
My question is how I break the loop as soon as I type the number 0.
For example, imagine that my vector have 50 positions and I type 10 positions with positive integers numbers and now I want the loop ends when I enter zero and the rest of the 40 positions remain blank.
I imagine it's with the while loop, but I could not even get close to the result I want.
#define MAX_SIZE (5)
int main()
{
int vector[MAX_SIZE] = {0};
int i;
memset(vector, 0xFF, sizeof(vector)); //sets all vector[i] to -1 on init;
for (i = 0; (i < MAX_SIZE) && (i?vector[i-1]:1) ; i++) //you can add the condition to break from the for loop here. This is more complicated but its just for demonstration.
{
printf("Number: ");
scanf_s("%d", &vector[i]);
}
system("pause");
return 0;
}
The above sample code demonstrates the using the for loops conditional expression as a means to break the loop. The ternary in there prevents the i=0 case resulting in an out of bounds index, due to the way you structured your loop. The better/more readable way is below:
#define MAX_SIZE (5)
int main()
{
int vector[MAX_SIZE] = {0};
int i;
memset(vector, 0xFF, sizeof(vector)); //sets all vector[i] to -1 on init;
for (i = 0; i < MAX_SIZE; i++)
{
printf("Number: %d\n", i);
scanf_s("%d", &vector[i]);
if(vector[i] == 0)
{
break;
}
}
//system("pause");
printf("Broke the loop when i was %d\n", i);
return 0;
}
The difference between the two besides readability is that i will be incremented one extra time in the first one. Also, make sure to initialize all the variables you declare (all your vector elements are stack garbage, the 0xFF memset assigns them all to -1).
Hi how would you count the number of occurences in the given word like shown below because with the program I have right now it doesn't seem to room correctly.
#include <stdio.h>
int main(void)
{
char a;
char lang[] = "pneumonoultramicroscopicsilicovolcanoconiosis";
char i = 0;
char count = 0;
printf("pneumonoultramicroscopicsilicovolcanoconiosis\n");
printf("\nEnter the letter you want to find the number of\n");
scanf("%c", &lang);
for (i = 0; i <= 46; i++)
if (a == lang[i]) {
count++;
}
printf("Number of %c is %d..\n", a, count);
return 0;
Your scanf is the problem.
Try:
scanf("%c",&a);
declare count as int instead of char
also change scanf to take input a
#include <stdio.h>
int main(void)
{
char a;
char lang[] = "pneumonoultramicroscopicsilicovolcanoconiosis";
char i = 0;
int count = 0;
printf("pneumonoultramicroscopicsilicovolcanoconiosis\n");
printf("\nEnter the letter you want to find the number of\n");
scanf("%c", &a);
for (i = 0; i <= 46; i++)
if (a == lang[i]) {
count++;
}
printf("Number of %c is %d..\n", a, count);
return 0;
}
You probably shouldn't hard code the max length of the string (46) if at all possible in case you are given a longer string, but assuming it's an assigned assignment that is set it shouldn't be a problem.
i and count should also be ints if possible for a bigger size count. And &lang should be &a since lang is already assigned while a is your checker.
I wanted to make a C program that finds the numbers in the input array and then multiplies it all together, I made the program but it got an issue that I don't know, can anybody help me with it!?
Here is the code!
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char t[10];
int n, z;
n = 0;
printf ("please enter a code: \n");
scanf ("%s", t);
while (n != '\0')
{
if (isdigit (t[n] == 0))
{
n++;
}
else
{
z = t[n];
z *= z;
}
}
printf ("%d", z);
}
Here is updated code. There is a comment for each bug that needed correction.
(Note that the comment describes the intention of the corrected code, it doesn't describe the bug.)
int temp;
z=1; // Initialize z
printf ("please enter a code: \n");
scanf ("%s", n);
while (t[n] != '\0') { // While there are more characters in the string
if (isdigit (t[n])) { // Check if the character is a digit
temp = t[n] - '0'; // Convert character digit to corresponding number.
z *= temp;
}
n++;
}
Your first problem is that you don't actually use t in your while loop. Your while loop only uses n which is set to 0 and never modified.
Your second problem is that you may be better off to use scanf("%d", &number); to scan numbers straight away.
z should be initialized to 1. and remove "z = t[n];"
#include <stdio.h>
#include <string.h>
main()
{
char a[5] ;
int b=1, n=0,m=0;
scanf("%s",a);
while (n <5 )
{
if (!isdigit(a[n]))
{
n++;
m++;
}
else{
b *= (a[n]-'0');
n++;
}
}
if(m==5) b=0;
printf("%d\n",b);
}