Problems with arrays - c

I know this is going to be something of a silly slip or oversight on my behalf, but I can't get the array in this to print out correctly. When I run the code and put in my inputs, I get seemingly random numbers.
For example,
number of rooms was 1
wattage of lights was 2
hours used was 2
TV/computers was 2
The output I got was 3930804. What did I miss?
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int room[20] = {0.0};
int i;
int rooms = 0;
char option = 0;
int lights[20];
int hrsUsed[20];
int Telly_Computer[20];
printf("Enter number of rooms");
scanf_s("%d", &rooms);
for(i=0;i<rooms;i++)
{
printf("input wattage of lights");
scanf_s("%d", (lights+i));
printf("input number of hours use/day (average)");
scanf_s("%d", (hrsUsed+i));
printf("input number of TV/Computers");
scanf_s("%d", (Telly_Computer+i));
}
printf("%d \n", lights);
}

printf("%d \n", lights);
You're printing the array directly. You need to loop over it and print the elements one at a time.
int i;
for (i = 0; i < 20; ++i)
printf("%d\n", lights[i]);

You are just printing the address of lights (and using UndefinedBehavior by the way, address must be printed with %p). You must use a loop to print out all of the contents of each array slot.
for(int i=0;i<(sizeof(lights)/sizeof(int));i++)
printf("%d\n",lights[i]);

Related

My code generates a random integer output and I do not know why

I am having difficulties with the output of a program for an assignment and I cannot figure out what is causing my problem. The code is as follows:
#include <stdio.h>
int find_minimum(int *a, int n)
{
int *last=(a + n);
int minimum = *a;
while(a!=last){
if(*a < minimum)
minimum=*a;
a++;
}
return minimum;
}
int main()
{
int N;
printf("Enter number of parts (N): ");
scanf("%d",&N);
int K;
printf("Enter number of part types (K): ");
scanf("%d",&K);
int a[K];
printf("Enter Part list:\n");
for(int i=0;i<N;i++){
int part;
scanf("%d",&part);
a[part-1]+=1;
}
printf("The factory can build %d computer(s)",find_minimum(a,K));
return 0;
}
This program is supposed to take input for the amount of parts and how many types of parts there are, and then calculate how many possible combinations of computers can be made out of the parts entered. The example input I was given looks like:
Enter the number of parts (N): 10
Enter the number of types of parts (K): 2
Enter part list:
1 1 1 1 1 2 2 2 2 2
The factory can build 5 computer(s)
Upon entering these numbers into the program, I get a randomly generated integer, which is not the intended result. Any help would be appreciated!
You declared your array but you did not initialize it. It means that every slot of your array does not have a 0 inside but can have a random value in it.
So doing a[part-1] += 1 add 1 to a random value (note that if part is 0 or > to k you are out of bound). You need to initialize every "slot" to 0.
try to compile and execute this to understand:
#include <stdio.h>
int main()
{
int arr[5];
for (int i = 0; i < 5; i++)
{
printf("%d\n", arr[i]);
}
}

Read a list of n numbers and make an array in C

So I want to take an input such as this:
The first input tells us the size of the array and the second line contains the numbers of array like this:
input:
3
1 2 3
And I want to make an array from the second input line with a size of from the first input line.
I currently have:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int n;
scanf("%d", n);
int x[n];
int y[n];
}
But after which I get stumped.
If you have a VLA(Variable Length Array) supporting compiler(eg. GCC):
int n;
scanf("%d", &n);
int arr[n];
scanf("%d %d %d", &arr[0], &arr[1], &arr[2]);
and if not,
int n;
scanf("%d", &n);
int *arr = malloc(n * sizeof(int));
scanf("%d %d %d", &arr[0], &arr[1], &arr[2]);
This code uses the functionality of scanf to be able to take multiple delimited input.
If you have to take n inputs and not only set the size of arr to n, do this:
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &n[i]);
}
It should be apparent that VLA functionality lets you to make an array on the stack with a runtime value. Otherwise, you'll need to allocate it on the heap with malloc().
What if there are more than 3 numbers to scan. Do I need to add more
"%d" and arr[] or is there some way for it to work with any number of
numbers in the second line.
To address this point you can go with iterating over loop
for(int i=0;i<n;i++)
{
scanf("%d",&array[i]);
}

How do you use scanf in a for loop so it does not stop looping on the first try?

for(int i=0;i<num; i++)
{
char word[32];
scanf(" %[^\n]s",word);
makeDictionary(word, readDictionary);
}
I have a program where I want to ask user for certain strings n times (with spacing allowed). However, when they input for example n = 2, it only loops once and exists. I know there is something wrong with my scanf.
I do Java and I'm a beginner at C. The way strings are done is very different.
This code can help you:
#include <stdio.h>
#include <string.h>
int main()
{
int N;
do
{
printf("Give me number of words :");
scanf("%d",&N);
}while(N<1);
for(int i=0;i<N;i++)
{char str[20];
printf("\nGive me the %d word :",i+1);
scanf("%s[^\n]",str);
printf("%s",str);
}
}

How to enter an output into an array in c

My Code so far:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <time.h>
int main()
{
int i;
int rollDice;
int firInp;
int secInp;
printf("Enter the amount of faces you want your dice to have (MAX=24, MIN=1): ");
scanf("%d", &firInp);
printf("Enter the amount of throws you want(MAX=499, MIN=1): ");
scanf("%d", &secInp);
srand ( time(NULL) );
if (((firInp < 25)&&(firInp > 1))&&((secInp < 500)&&(secInp > 1))){
for(i = 0; i < secInp; i++){
rollDice = (rand()%firInp) + 1;
printf("%d \n", rollDice);
}
}
else{
printf("Sorry, these numbers don't meet the parameters. Please enter a number in the right parameters.");
}
return 0;
}
I want to have percentages in my code. I think the way to do that is to first enter the output of my code into an array. If there's another way please feel free to let me know.
edit: I want the output to be something like this:
1 3 4 4 4 5
occurrence of 1: 16.6 percent
occurrence of 3: ..and so on
Instead of entering the output of the random function in an array you could just use that array as a counter, and incrementing the array at position rollDice every time a number appears. Than you could easily extract the percentage by summing all the elements of the array and by dividing each element by that sum.
You can create an array of integers, with size equals to the numbers of possible values your dice can output. Then you use it as a counter for the number of occurences you get, the index of the array will represent that output value (you can use rollDice-1 since 0 isn't a possible output of the dice), and the value at the index will be the number of occurences.
After you finish rolling the dice you just have to print the percentage like this:
for (int i=0;i<firInp;i++) { // firInp: n_faces = n_possible_values
printf("Occurrence of %d: %.1f percent\n", i+1, ((float)array[i]*100)/(float)secInp);
}

Can't find the error in minimum difference between two numbers on array in C

So I'm really new to programming. I need to write a program that, if I give it any array of integers, it'll be able to find the two numbers closest to each other, and then give the difference between those two numbers. Also, the first number must be the number of integers that are going to be in the array.
So for example, I give it 3 1 4 8. The first 3 means that there will be three integers, so it must find the closest two numbers between those three. In this case, it's 4 - 1 = 3, so the output should be 3, but when I write it it gives me 16.
This is what I have, and I don't know what's wrong:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("Write numbers here\n");
scanf("%d", &n);
int st[n];
for(i=0;i<n;i++)
scanf("%d",&st[i]);
int a, b, str[n*n], minimum, c;
/* here I'll make a new array, and its elements will be all the
differences between all the elements of the previous one */
for(a=0;a<n;a++)
for(b=0+a*n;b<n;b++) {
if(st[b-a*n]==st[a])
str[b]=32000;
else
str[b]=abs(st[b-a*n]-st[a]);
}
// here I'll find the smallest element on the last made array
minimum = str[0];
for(c=0;c<n*n;c++)
{
if(str[c]<minimum);
{
minimum=str[c];
}
}
printf("%d", minimum);
return 0;
}
Edit: I tried to fix it with your answers but it still doesn't work.
New code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("Write numbers here\n");
scanf("%d", &n);
int st[n];
for(i=0;i<n;i++)
scanf("%d",&st[i]);
int a, b, minimum;
minimum = st[0];
for(a=0;a<n;a++)
for(b=0;b<n;b++) {
if((st[b] != st[a]) && (abs(st[b]-st[a]))<minimum)
minimum = abs(st[b]-st[a]);
}
printf("%d", minimum);
return 0;
}
Edit 2: Ok, I fixed it now. Thanks a lot ^^
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main() {
int n, i;
printf("write numbers here\n");
scanf("%d", &n);
int st[n];
for(i=0;i<n;i++)
scanf("%d",&st[i]);
int a, b, minimum;
minimum = INT_MAX;
for(a=0;a<n;a++)
for(b=a+1;b<n;b++) {
if((abs(st[b]-st[a]))<minimum)
minimum = abs(st[b]-st[a]);
}
printf("%d", minimum);
return 0;
}

Resources