While-loops school assignment - loops

This assignment consists of two tasks.
Task 1:
a) Create a program that reads numbers from the user until the user provides the number 0. (Without doing anything with numbers). Use a while loop to achieve this.
I made this code to achieve this:
import java.util.Scanner;
public class HelloPrinter {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int ditt_tall = 1;
while(ditt_tall != 0) {
System.out.print("Skriv inn ditt tall: ");
ditt_tall = in.nextInt();
}
}
}
This is the task in task 2:
b) Expand the program to sum all the numbers from the user (until user
give the number 0) and write the result to the terminal
To be honest. I have no idea what to do. Can someone give me a hint or expand the first code so it works the way it should?

Make a variable to save the total, before your while loop starts and set it to zero. Inside your loop, accept the input and add it to your total value. After the loop ends, print out your total variable value.
So, in addition to your
int ditt_tall = 1;
create your total variable there too, like
int ditt_total = 0;
inside your loop, you can increment the total with the value that is input:
ditt_total = ditt_total + ditt_tall;
And after the loop ends, print out the value of ditt_total.
Go through the code by hand, pretending to input numbers, and seeing how ditt_tall and ditt_total change with each loop. Using paper is a good way to understand what the program is doing, and also helps figure out what is going wrong when the results are not what you expect.
Here are some things to consider, however: what happens if the user enters a string or decimal number? Maybe at this point you don't have to deal with that, but bad input is a problem you'll always need to consider in real life.

Related

why is my If condition not working? ( C language)

I'm making a train scheduling program. In my If condition, I only want to accept 10 train ids between 100 and 200. My program is running on command prompt but my If condition is totally ignored. How should I fix this so that my program will accept ids between 100 and 200 and request the user to enter the train id again if it's not obeying the condition?
My Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int train_id;
int train_time;
} TrainDetails;
void enterID(TrainDetails array[10]);
int main()
{
TrainDetails array[10];
enterID(array);
return 0;
}
void enterID(TrainDetails array[10])
{
int count = 1;
int enter_Tid;
while(count < 11)
{
printf("Enter Train ID :\n");
scanf("%d",&enter_Tid);
if(enter_Tid>100 && enter_Tid<200)
{
array[count].train_id = enter_Tid;
}
count++;
}
}
you should implement a slightly different approach (it was posted on a different post. Not the same thing but a similar structure that you can use: how to use a while loop to keep asking for input until the input is correct (C)?).
These are basic control flow structures, so I suggest reading a good beginners book on the basics of programming.
int tid = -1;
while (true){
printf("input an integer value: ");
scanf("%d",&tid);
if (tid > 100 && tid < 200 ){
printf("successfully read an item");
break;
} else {
printf("Invalid tid. Retry.");
}
}
If you don't want to use the break statement you can use this kind of basic control flow:
do {
printf("input an integer value: ");
scanf("%d",&tid);
} while (tid < 100 || tid > 200);
These should be inside a bigger loop where you decide how many of these train IDs you need.
Your program does not produce any output besides the "Enter Train ID" prompts, so the only way I can imagine for you to have concluded that your if condition is being ignored is by counting the number of prompts emitted before the program terminates. Seeing only ten total even in the event that you enter an out-of-range train id, you assume that the if condition that you are using to validate the inputs has been ignored.
But that depends on an aspirational expectation of how your program will behave, not a reasonable expectation of the code you actually presented. Consider, what work is performed by the code inside the if statement's body? Only one thing: assigning the entered ID to a train. So if the user enters an invalid ID then that will not happen, but everything else in the loop body still will. In particular, count is incremented at the end of the loop regardless of whether the input was valid.
You would have had more of a clue about what is going on if you had provided more feedback to the user. It is very unfriendly to reject an input without giving any notice that you have done so or any way for the user to recognize it. At least two reasonable feedback mechanisms could be easily integrated:
Update the prompt to tell the user for which train an ID is being requested:
printf("Enter the ID for Train %d :\n", count);
Add an else block to your validation test that, when executed, prints a message explaining that the ID was rejected.
I would probably do both, but the latter is the one that would make it clear that your if condition itself is working as expected. The former would help you recognize that the loop requests an ID only once for each train, regardless of whether a valid one is provided. It stores only valid IDs, but it never prompts for a replacement ID for an invalid one.

How to track amount of inputs that have been inputted by a user? C Program

EDIT: I am so sorry for the inconvenience but I had to take out the original code that I had used to submit this question. I will reupload my old code and the new code later! But the answer that is still up was very helpful in understanding how to track the number of inputs that are inputted by a user using a do ... while loop.
For this assignment, we were supposed to create a random guessing game where user inputs a number between 1 and 100 and has 3 functions (int getRandomNumber(void), int check4Win(int,int) and void printResults(int)). getRandomNumber was just made to create the random number, check4Win was just to tell the user if their guess is too high, too low, or correct and for printResults we are supposed to rank the user based on how many guesses they inputted. My issue is with printResults and what goes in main to actually keep track of how many guesses the user inputted. I was able to get the entire code to work but my main issue is being able to keep track of the number of inputs because what I have now is printing out the first input rather than the number of inputs. I will include my entire code but the main issue is printResults. The rest of them seem fine but if you think there is an issue somewhere else please let me know! I know that I used a switch statement inside the for statement in main and I am about 99.9% sure that is wrong I just couldn't figure out what else to do. Also, the formatting is off a bit here but that's just so that the code can stay together its formatted fine on my compiler! I am also fairly new to so I will take all criticism.
Thank you!
Here is a very simple example of how to count "guesses" until success. You can extend as you see fit, whether that is to move stuff into functions, add "maximum guesses" logic, use proper input handling (this will break if you input a non-integer, like hello).
int randNum = getRandomNumber();
int guessNum = 0;
int guessCount = 0;
do {
printf("Guess #%d: ", guessCount + 1);
scanf("%d", &guessNum);
// validate
if (guessNum < 1 || guessNum > 100)
{
printf("Invalid guess\n");
continue;
}
++guessCount;
// check
if (guessNum < randNum)
printf("Too low\n");
else if (guessNum > randNum)
printf("Too high\n");
} while(guessNum != randNum);
printf("Total guesses: %d\n", guessCount);
My advice is you manage input in one place. I couldn't stomach the way you're doing input in main, but then the "check4win" function tries to input values too. A function should do what its name implies. That function "checks" something, so one would expect that it returns some kind of value that indicates the result of "checking". That's all it should do. Since it's called "check4Win", I would expect it returns true if the guess "wins" and false otherwise.

How do I generate random numbers but not repeating and within a certain range based on user input?

Currently, I am experimenting some codes regarding/about random numbers. The problem is, when I run the program, input any number (ex. 12), I, sometimes get the correct and sometimes wrong answer. The correct answer must be any non repeating random numbers based on user input. (ex. input 5, output must be 1. seats[12]=1, 2. seats[19]=1,..., 5. seats[47]=1). I do not know what to do and yeah help me coders!
Here's the code:
#include<stdio.h>
#include<conio.h>
#include<time.h>
main()
{
int x,y,chc1A,seats[50]={};
printf("Enter a number: ");
scanf("%d",&chc1A);
srand(NULL);
for(x=0;x<chc1A;x++)
{
if(seats[rand()%50]==0)
seats[rand()%50]=1;
else
x--;
}
for(x=0,y=1;x<50;x++)
if(seats[x]==1)
{
printf("%d. seats[%d] = %d\n",y,x,seats[x]);
y++;
}
getch();
}
I do not really know what's wrong please enlighten me.
I run and coded this on Dev C++
What I want to do is: generate random numbers between 0-49 and putting it in an array seats[50]. (ex 38 then put 1 in array seats[38]). Btw this code represents passengers sitting on a bus with 50 seats. So the "1" means that the seat is occupied.
This part may cause problem.
for(x=0;x<chc1A;x++)
{
if(seats[rand()%50]==0)
seats[rand()%50]=1;
else
x--;
}
I think by
if(seats[rand()%50]==0)
seats[rand()%50]=1;
you meant to generate a random number, use that as index to seats and if seats[random_no] is 0, set seats[random_no] to 1.
But the random number in the if statement and the one in its body are different numbers.
You could use
int index = rand()%50;
if(seats[index]==0)
seats[index]=1;
Consider changing the signature of your main() function. See What are the valid signatures for C's main() function?
conio.h is not part of the standard, try to avoid its usage. Same goes for getch() as it's from conio.h.
srand() expects an unsigned int as argument and NULL is not of that type. See here.
Include stdlib.h to use srand().
And checking the return value of scanf() is a good idea. You can see if it failed or not.

Searching for all integers that occure twice in a vector

I got the task in university to realize an input of a maximum of 10 integers, which shall be stored in a one dimensional vector. Afterwards, every integer of the vector needs to be displayed on the display (via printf).
However, I don't know how to check the vector for each number. I thought something along the lines of letting the pointer of the vector run from 0 to 9 and comparing the value of each element with all elements again, but I am sure there is a much smarter way. I don't in any case know how to code this idea since I am new to C.
Here is what I have tried:
#include <stdio.h>
int main(void)
{
int vector[10];
int a;
int b;
int c;
a = 0;
b = 0;
c = 0;
printf("Please input 10 integers.\n\n");
while (a <= 10);
{
for (scanf_s("%lf", &vektor[a]) == 0)
{
printf("This is not an integer. Please try again.\n");
fflush(stdin);
}
a++;
}
for (b <= 10);
{
if (vector[b] != vector[c]);
{
printf("&d", vector[b]);
c++;
}
b++;
}
return 0;
}
Your code has several problems, some syntactic and some semantic. Your compiler will help with many of the former kind, such as
misspelling of variable name vector in one place (though perhaps this was a missed after-the-fact edit), and
incorrect syntax for a for loop
Some compilers will notice that your scanf format is mismatched with the corresponding argument. Also, you might even get a warning that clues you in to the semicolons that are erroneously placed between your loop headers and their intended bodies. I don't know any compiler that would warn you that bad input will cause your input loop to spin indefinitely, however.
But I guess the most significant issue is that the details of your approach to printing only non-duplicate elements simply will not serve. For this purpose, I recommend figuring out how to describe in words how the computer (or a person) should solve the problem before trying to write C code to implement it. These are really two different exercises, especially for someone whose familiarity with C is limited. You can reason about the prose description without being bogged down and distracted by C syntax.
For example, here are some words that might suit:
Consider each element, E, of the array in turn, from first to last.
Check all the elements preceding E in the array for one that contains the same value.
If none of the elements before E contains the same value as E then E contains the first appearance of its value, so print it. Otherwise, E's value was already printed when some previous element was processed, so do not print it again.
Consider the next E, if any (go back to step 1).

Why am I getting this huge numbers in C?

I'm trying to teach myself C using Kernighan's book and I'm supposed to make a graph that indicates how many letters there are in each word. I haven't got to the "plotting" part as I'm getting really weird and enormous numbers at the output. Right now I'm just trying to have the value of the first and second elements of the array "arreglo" printed.
The code is the following:
#include <stdio.h>
#define ARRAY_SIZE 100
/*#define AUX 0
#define AUX2 0*/
main()
{
int s,j,noletra,i,arreglo[ARRAY_SIZE],otros, nopalabra, c;
int a;
nopalabra=1;
while((c=getchar())!=EOF)
{
if(c==' '||c=='\t'||c=='\n')
++nopalabra;
else
++arreglo[nopalabra];
}
printf("%d",arreglo[1],arreglo[2]);
}
The reason I'm trying to know the value in the second element in the array is that the first element has the correct value. The code is supposed to add 1 to the array index which is the number of words each time a space, tab or \n is typed and to add 1 to the array element whenever something different than the previously mentioned characters is typed (Letters). Right now it´s supposed to print correctly the number of the letters of two words, the first element is correctly printed but the second is a huge number, the output is:
alan amaury
^Z
4 8257542
--------------------------------
Process exited after 7.773 seconds with return value 9
Press any key to continue . . .
The output is supposed to be 4 7. I'm using a compiler in windows so EOF should be Ctrl+Z
Any help that I could get from you will be appreciated :)
At least these problems.
int arreglo[ARRAY_SIZE]; is not initialized before its elements are incremented. This is the mostly likely cause of "Why am I getting this huge numbers" #ikegami
// int arreglo[ARRAY_SIZE];
// replace with
int arreglo[ARRAY_SIZE] = { 0 };
Code can access out of array bounds as nopalabra is not confined to 0 to 99.
#define ARRAY_SIZE 100
int arreglo[ARRAY_SIZE];
++arreglo[nopalabra]; // No access protection
printf("%d",arreglo[1],arreglo[2]); only prints arreglo[1]
Logic flow is incorrect to "make a graph that indicates how many letters there are in each word."
main() return type not coded.
Some pseudo-code as an alternative
int main(void) {
set all arreglo[] to 0
int nopalabra = 0;
part_of_word = false; // Keep track of word state
loop forever {
get a character into c
if c is a separator or EOF
if (part_of_word) {
arreglo[nopalabra]++;
part_of_word = false;
nopalabra = 0;
if (c == EOF) break loop
} else {
nopalabra++;
part_of_word = true;
}
}
print out results
}
First, try the solution answered before, changing the printf() call.
If there is still a problem try:
printf("%d %d \n",arreglo[1],arreglo[2]);
Just before the while loop, to see if the "arreglo" array is initialize to 0's or just random values.
On the side:
Your call to printf() has more parameters than covered by the format string.
So you should clean up your code to something similar to
printf("%d %d\n", arreglo[1], arreglo[2]);
Concerning the strange output:
A way of getting surprising values is using non-initialised variables.
In your case the lack of initialisation affects the array arreglo.
Make sure to initialise it, so that all counting starts on a meaningful value.
Another way of getting seemingly very high values is printing several numbers next to each other, without separating white space in between.
So the " " and the "\n" in the format string I proposed are quite relevant.
the question is: "Why am I getting this huge numbers in C?"
the answer is: you have not assigned anything to arreglo array, and since you have not initialized it, you got some random values that have been there in memory. Trust me on this, I have run your code in debugger, which I also highly recommend a a standard practice while lerning to program.
Also, this is a common mistake where the format specifier is not written:
printf("%d",arreglo[1],arreglo[2]);
See if using printf("%d","%d",arreglo[1],arreglo[2]) solves your problem.
By the way, K&R book is very old, and written at the time where there have been no other C books. There are better ways to learn C.

Resources