for our homework we have to compile the program we wrote in the school. I have typed it without mistakes(verified with my colleagues) and the program does not work, I am using DEV C++ and the error log says, file not recognized: File format not recognized.
I tried using integer and not double but it stays the same...I have no idea what is wrong.
#include <stdio.h>
#define VELIKOST 23
int main (void)
{
double dPolje[VELIKOST];
int iStevec,iVecje=0;
printf("Algoritem, ki določi koliko elementov podatkovnega polja imajo vrednosti vecje ali enake od 10 \r\n");
for(iStevec=0;iStevec<VELIKOST;iStevec++)
{
printf("Vnesite %i. stevilo:",iStevec=iStevec+1);
fflush(stdin);
scanf("%lf",&dPolje[iStevec]);
if(dPolje[VELIKOST]>=10)
{
iVecje++;
printf("Element dPolje [%i]=%f.",iStevec,dPolje[iStevec]);
}
printf("%i elementov polja je imelo vecje ali enako vredost 10.",iVecje);
return(0);
}
}
I'm guessing that Dev C++ doesn't support Slovenian.
Create a new file and try this code:
#include <stdio.h>
#define SIZE 23
int main(){
double dField[SIZE];
int i, larger = 0;
printf("This algorithm, determines how many data field items have values greater than or equal to 10.\n");
for (i = 0; i < SIZE; i++){
printf("Enter field number %i:", i + 1); //Note I fixed this original code had i = i + 1
//fflush(stdin); unneeded
scanf("%lf", &dField[i]);
if (dField[i] >= 10){
larger++;
printf("Field number %i = %lf", i, dField[i]);
}
} //Moved this above final output and return
printf("%i field items were greater than or equal to 10 ", larger);
return 0;
}
I expect that to work.
Either way I'd definitely change compilers. Visual Studio Community Is a great fully featured IDE.
Related
I need to print the sum and average of user input array. So if user inputs 2,4,6,9,10 it should print 6.
However, after the loop ended my printf is not printing anything.
Even if I put the printf inside the array it only prints out 0.
#include <stdio.h>
#include <math.h>
int main()
{
int i;
double num[6],average, sum=0, closest;
printf("Enter 6 doubles\n");
for (i=0; i<6; i++)
{
scanf("%lf",&num[i]);
sum += num[i];
}
average = sum/i;
printf("Average %d", average);
}
There are a few things you need to do in the code. You should be making sure they enter 6 numbers (in your opening post you only list 5, this will create problems). I changed the printing to use this and stripped out some variables that you don't use.
#include <stdio.h>
#include <math.h>
int main()
{
int i;
double sum = 0;
printf("Enter 6 doubles\n");
for (i = 0; i < 6; i++)
{
double value;
scanf("%lf", &value);
sum += value;
}
printf("Average = %f", sum / i);
}
Enter 6 doubles:
2 4 6 9 10 10
Average = 6.833333
This question is not a duplicate but I found the answer on StackOverflow here
The stdout stream is buffered, so will only display what's in the
buffer after it reaches a newline (or when it's told to). You have a
few options to print immediately:
Print to stderr instead using fprintf:
fprintf(stderr, "I will be printed immediately");
Flush stdout whenever you need it to using fflush:
printf("Buffered, will be flushed"); fflush(stdout); // Will now print everything in the stdout buffer
You can also disable buffering
on stdout by using setbuf:
setbuf(stdout, NULL);
Then regarding your code, here are a few remarks:
As described in man 3 printf the conversion specifier f already convert to double floating point values, so no need for a length modifer flag.
The average value is also a double so if you print it as an integer %d you will lose the real valued part, consider using %f as well.
the following proposed code:
uses a proper signature for main()
corrects the format used in the call to printf()
appends a '\n' to the format string in 'printf()' so the data is immediately output rather than after the program exits
gives 'magic' numbers (I.E. 6) meaningful names
properly checks for I/O errors and handles any such error
eliminates unneeded variables
does not include header files those contents are not used
documents why each header file is included
properly limits the scope of local variable 'i'
cleanly compiles
performs the desired functionality
and now, the proposed code:
#include <stdio.h> // printf(), scanf(), perror()
//#include <math.h>
#include <stdlib.h> // exit(), EXIT_FAILURE
#define MAX_ENTRIES 6
int main( void )
{
//int i;
// double num[6];
double num;
double average;
double sum=0.0;
// double closest;
printf("Enter %d doubles\n", MAX_ENTRIES );
for (int i=0; i< MAX_ENTRIES; i++)
{
if( scanf( "%lf", &num ) != 1 )
{
fprintf( stderr, "scanf for number failed\n" );
exit( EXIT_FAILURE );
}
sum += num;
}
average = sum / MAX_ENTRIES;
printf("Average %f\n", average);
}
a typical run of the code results in:
Enter 6 doubles
1.0
2.0
3.0
4.0
5.0
6.0
Average 3.500000
I am able to write a program to find the mode provided there is only one mode. However, I'm not sure how to alter my code so it can function properly when there is more than one mode.
Here is what I have! Any advice on how to fix my code would be appreciated.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main ()
{
int x, i, c[101], mode;
printf("please enter test scores between 0 and 100\n");
i = 0;
mode = 0;
while (i<=100)
{
c[i]=0;
i=i+1;
}
scanf("%d", &x);
while ((x>=0) && (x<=100))
{
c[x]=c[x]+1;
if (c[x]>=mode)
{mode=x;}
scanf("%d", &x);
}
printf("the mode is %d\n", mode);
}
You'd want to:
a) Have something that keeps track of how often each value occurred. You're already using an array of "occurrence counts" for this.
b) Find the highest "occurrence count"
c) Find all values that share the highest "occurrence count"
For your code, this can mostly be done by replacing:
if (c[x]>=mode)
{mode=x;}
..with something more like:
if (c[x] > highest)
{highest = c[x];}
..and then doing something like this at the end:
printf("the mode/s are:");
for(i = 0; i <= 100; i++) {
if(c[i] == highest) {
printf(" %d", i);
}
}
printf("\n");
probably another dumb error but I really can't wrap my head around this.
I'm writing a basic polynomials class, and my program suddenly crashes upon input of a couple of ints.. I tried searching for a solution but I couldn't find one :/
The code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Homework.h"
#include "Fraction.h"
int main()
{
//Input from user
int degree, i;
printf("Insert the degree of the polynomomial: \n");
scanf("%d", °ree);
//Get the coefficcients
struct fraction *bucket = malloc((sizeof(struct fraction))*(degree + 1));
int num;
unsigned int den;
for(i = 0; i < degree + 1; i++)
{
num = 0;
den = 1;
printf("Insert the coefficcient of degree %d, first num and afterwards
the den \n", i);
printf("Numerator:\n");
if(scanf("%d", &num) != 1)
printf("Input error\n");
printf("Denominator:\n");
if(scanf("%u", &den) != 1)
printf("Input error\n");
//struct fraction temp = {num, den};
//memcpy(&bucket[0], &temp, sizeof(struct fraction));
}
//Check insertion
printf("Test\n");
//print_fraction(bucket[0]);
}
The program exits even before printing "Test", and to input I am using input number + enter key.
Thanks very much for any help!
Proof it compiles
Your code seems to be working fine.
The only changes i've made in order to compile it was to comment out the line where you are using malloc, and also brought your print statement on one line.
If you are using a newer version of Visual Studio then it will give you issues when using the scanf function. You either have to use scanf_s or disable the warning with this line at the top:
#pragma warning(disable: 4996)
Hope this helps.
I've trying to do it for about an hour, but I can't seem to get it right. How is it done?
The code I have at the moment is:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int j=-1;
while(j<0){
printf("Enter a number: \n");
scanf("%d", &j);
}
int i=j;
for(i=j; i<=100; i++){
printf("%d \n", i);
}
return 0;
}
The original specification (before code was added) was a little vague but, in terms of the process to follow, that's irrelevant. Let's assume they're as follows:
get two numbers from the user.
if their product is greater than a thousand, print it and stop.
otherwise, print product and go back to first bullet point.
(if that's not quite what you're after, the process is still the same, you just have to adjust the individual steps).
Translating that in to pseudo-code is often a first good step when developing. That would give you something like:
def program:
set product to -1
while product <= 1000:
print prompt asking for numbers
get num1 and num2 from user
set product to num1 * num2
print product
print "target reached"
From that point, it's a matter of converting the pseudo-code into a formal computer language, which is generally close to a one-to-one mapping operation.
A good first attempt would be along the lines of:
#include <stdio.h>
int main (void) {
int num1, num2, product = -1;
while (product < 1000) {
printf ("Please enter two whole numbers, separated by a space: ");
scanf ("%d %d", &num1, &num2);
product = num1 * num2;
printf ("Product is %d\n", product);
}
puts ("Target reached");
return 0;
}
although there will no doubt be problems with this since it doesn't robustly handle invalid input. However, at the level you're operating, it would be a good start.
In terms of the code you've supplied (which probably should have been in the original question, though I've added it now):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int j=-1;
while(j<0){
printf("Enter a number: \n");
scanf("%d", &j);
}
int i=j;
for(i=j; i<=100; i++){
printf("%d \n", i);
}
return 0;
}
a better way to do the final loop would be along the lines of:
int i = 1;
while (i < 1000) {
i = i * j;
printf ("%n\n", i);
}
This uses the correct terminating condition of the multiplied number being a thousand or more rather than what you had, a fixed number of multiplications.
You may also want to catch the possibility that the user enters one, which would result in an infinite loop.
A (relatively) professional program to do this would be similar to:
#include <stdio.h>
int main (void) {
// Get starting point, two or more.
int start = 0;
while (start < 2) {
printf("Enter a number greater than one: ");
if (scanf("%d", &start) != 1) {
// No integer available, clear to end of input line.
for (int ch = 0; ch != '\n'; ch = getchar());
}
}
// Start with one, continue while less than a thousand.
int curr = 1;
while (curr < 1000) {
// Multiply then print.
curr *= start;
printf ("%d\n", curr);
}
return 0;
}
This has the following features:
more suitable variable names.
detection and repair of most invalid input.
comments.
That code is included just as an educational example showing how to do a reasonably good job. If you use it as-is for your classwork, don't be surprised if your educators fail you for plagiarism. I'm pretty certain most of them would be using web-search tools to detect that sort of stuff.
I'm not 100% clear on what you are asking for so I'm assuming the following that you want to get user to keep on entering numbers (I've assumed positive integers) until the all of them multiplied together is greater than or equal to 1000).
The code here starts with the value 1 (because starting with 0 will mean it will never get to anything other than 0) and multiples positive integers to it while the product of all of them remains under 1000. Finally it prints the total (which may be over 1000) and also the number of values entered by the user.
I hope this helps.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char input[10];
unsigned currentTotal = 1;
unsigned value;
unsigned numEntered = 0;
while( currentTotal < 1000 )
{
printf( "Enter a number: \n" );
fgets( input, sizeof(input), stdin );
value = atoi( input );
if( value > 0 )
{
currentTotal *= value;
numEntered += 1;
}
else
{
printf( "Please enter a positive integer value\n" );
}
}
printf( "You entered %u numbers which when multiplied together equal %u\n", numEntered, currentTotal );
return 0;
}
Try this one:
#include <stdio.h>
int main()
{
int input,output=1;
while(1)
{
scanf("%d",&input);
if(input<=0)
printf("Please enter a positive integer not less than 1 :\n");
else if(input>0)
output*=input;
if(output>1000)
{
printf("\nThe result is: %d",output);
break;
}
}
return 0;
}
I'm relatively new to C programming, its my 6th week in class so far i haven't had any major issues. I just cant figure out were i'm going wrong with my current assignment and its due in just a couple hours. Here is what i have so far. i'm using visual studio 2012.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char textChar;
int textLenght = 0;
int asciiArray[128] = {0};
int i;
int main()
{
printf("Enter a line of text: ");
scanf("%d", &textChar);
while ((textChar = getchar())!= '\n') {
textLenght++;
asciiArray[textChar]++;
}
printf("\nFREQUENCY TABLE\n");
printf("---------------\n");
printf("Char Count %% of Total\n");
printf("---- ----- ----------\n");
printf(" ALL %5d %9.2f%%\n", textLenght,( textLenght * 100.0 ) / textLenght );
for (i = 0; i < 128; i++)
if( asciiArray[textChar] != 0 )
printf("%c %d %9.2f%% \n",i+ "0",asciiArray[textChar]);
getchar();
getchar();
return 0;
}
Now i know there is a problem within my for loop because its not displaying, I'm just not sure if there are other problems besides that. Any help is greatly appreciated thanks in advance.
This line is not right.
scanf("%d", &textChar);
It's not clear to me what you are trying to accomplish with this line.
When you use %d as the format specifier, the function will try to read an integer and store it at the given address. Since type of textChar is not int, you are going to run into undefined behavior right away.
Instead of using getchar, which is not a standard C library function, you should use fgetc(stdin).
fgetc() returns an int. Make sure to change the type of textChar to int.
Change the lines:
printf("Enter a line of text: ");
scanf("%d", &textChar);
while ((textChar = getchar())!= '\n') {
textLenght++;
asciiArray[textChar]++;
}
to
printf("Enter a line of text: ");
while ((textChar = fgetc(stdin))!= '\n') {
textLenght++;
asciiArray[textChar]++;
}
I would remove the last two calls to getchar(). They don't seem to serve any purpose.