When trying to read about 2000 int numbers, scanf() stops working after 1040 ints. How can I solve this problem?
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main() {
int length;
scanf("%d", &length);
int32_t *array = (int32_t *) calloc(length, sizeof(int32_t));
for (int32_t i = 0; i < length; i++) {
scanf("%d", &array[i]);
}
printf("\nAll data are scanned\n");
for (int32_t i = 0; i < length; i++) {
printf("%d, ", array[i]);
}
free(array);
return 0;
}
Related
I trying to copy Char array elements to Int type array.... I defined size as 10 but when i put 10 character (a ~ j),the code is stop and get out from running...
#include <stdio.h>
#define size 10
int main(void){
char charr[size];
printf("input your string\n");scanf("%s", charr);
printf("%s", charr);
int arr[size];
for(int i = 0; i<size; i++){
arr[i] = (int)charr[i];
}
printf("\n");
for (int i = 0; i < size; i++){
printf("%d ", arr[i]);
}
return 0;
}
I am writing a C program that should sort floats (increasingly). The values are saved in a file and then after the sort, the values are saved into the same file. I am using library functions to read and write. I am using radixsort to sort the floats.
This is the code I currently have. Rather than the array being populated with values from the file that I am reading, it is storing 0.000000 for every index. I am not sure where I am going wrong in my implementation.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
void swap(float *a, float *b, size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
float tmp = a[i];
a[i] = b[i];
b[i] = tmp;
}
}
void radixSort(float array[], size_t count)
{
int numZeroes=0;
float tempArr1[count];
float * tempArr2 = tempArr1;
for (uint32_t radix=1;radix;radix<<=1){
uint32_t * intArray = (uint32_t *)array;
int count0=0;
int count1=0;
numZeroes=0;
for (int j=0; j<count; ++j)
numZeroes += !(intArray[j]&radix);
count1=numZeroes;
for (int j=0; j < count; ++j)
if (intArray[j]&radix){
tempArr2[count1]=array[j];
++count1;
}
else{
tempArr2[count0]=array[j];
++count0;
}
swap(tempArr2,array,count);
}
if (numZeroes<count){
memcpy( tempArr2+(count-numZeroes), array, numZeroes*sizeof(float));
for (int d=0,j=count-1;j>=numZeroes;j--,d++)
tempArr2[d]=array[j];
memcpy( array, tempArr2, count * sizeof(float));
}
}
int main(int argc, char *argv[]) {
FILE *fd, *writeFile;
int i = 0;
float number;
int elementnum = 0;
struct stat st;
int fd2;
fd=fopen(argv[1], "r");
fd2 = fileno(fd);
if(fd==NULL){
printf("Error opening file\n");
}
fstat(fd2, &st);
off_t size = st.st_size;
for(int j = 0; j < size/4; j++){
elementnum++;
}
float array[elementnum];
while(fscanf(fd, "%f", &number)==1) {
array[i] = number;
i++;
}
radixSort(array,elementnum);
for(int j = 0; j < elementnum; j++){
printf("%f\n", array[j]);
}
fclose(fd);
writeFile=fopen("argv[1]", "w");
for(int j = 0; j < elementnum; j++){
fprintf(writeFile, "%f\n", array[j]);
}
fclose(writeFile);
return 0;
}
At least these issues:
Code is assuming the size of the text file divided by 4 is the number of float. This is risky and error prone. #agus
Instead of fstat(fd2, &st);, count them
size_t elementnum = 0;
while(fscanf(fd, "%f", &number)==1) {
elementnum++;
}
rewind(fd);
float array[elementnum];
for (i=0; i < elementnum; i++) {
if (fscanf(fd, "%f", &number) != 1) {
puts("We have trouble reading the same on the 2nd pass");
exit(1);
}
array[i] = number;
}
radixSort(array, elementnum);
/ 4 might make sense with a binary file and using fread() to read float.
it is storing 0.000000 for every index
Writing float with "%f" is uninformative with small values. Better to use "%e" or "%g".
#include <float.h>
// fprintf(writeFile, "%f\n", array[j]);
fprintf(writeFile, "%g\n", array[j]);
// or better
fprintf(writeFile, "%.*g\n", FLT_DECIMAL_DIG, array[j]);
I have an array
arr[]={7,5,-8,3,4};
And I have to update the same array to
arr[]={7,12,4,7,11};
my code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int sumArr(int *arr, int size);
void main()
{
int arr[] = { 7,5,-8,3,4 };
int i, size, res = 0;
printf("Enter Size Of The Array:");
scanf("%d", &size);
res = sumArr(arr, size);
for (i = 0; i < size; i++)
{
printf("%d\n", res);
}
}
int sumArr(int *arr, int size)
{
int i;
for (i = 0; i < size; i++)
{
arr[i+1]+= arr[i];
printf(" %d \n", arr[i + 1]);
}
return arr[i+1];
}
The output should be: 7,12,4,7,11
But in my code, the output is: 12,4,7,11,-858993449,58196502,58196502,58196502,58196502,58196502
Any hints?
I can use auxiliary functions for input and output arrays, will it help?
You have several mistakes in your code:
You need to stop the summing loop once i+1 reaches the end of the array
Your code knows the size; there is no need to read it from end-user
You need to print the value of res once, rather than printing it in a loop
You should consider moving the printing portion of the program into main from sumArray.
The modifications are very straightforward:
int sumArr(int *arr, int size) {
// Stop when i+1 reaches size; no printing
for (int i = 0; i+1 < size; i++) {
arr[i+1]+= arr[i];
}
return arr[size-1];
}
Printing in the main:
printf("sum=%d\n", res);
for (int i = 0; i < size; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
Demo.
#include "stdafx.h"
#include "stdio.h"
#include <string.h>
void main() {
int Results[8];
int i = 0;
int max = 0;
int maxindex;
printf("Enter the results of your 7 leavin cert subjects: ");
do {
printf("\nSubject %d: ", i + 1);
scanf_s("%d", Results);
i++;
} while (i < 7);
for (i < 7; Results[i] > 0; i++)
if (Results[i] > max)
max = Results[i];
printf("The best grade is %d", max);
}
Hello, so basically I'm trying to print out the largest number(Best result) by using a for loop. However it keeps telling me the that the best result is 0.
Does anybody know what I'm doing wrong. Any help would be greatly appreciated.
There are 2 major problems in your code:
You read all numbers into Results[0] with the scanf_s("%d", Results);. You should instead write:
if (scanf_s("%d", &Results[i]) != 1) {
/* not a number, handle the error */
}
The second loop is incorrect: for (i < 7; Results[i] > 0; i++) has multiple issues. Write instead for (i = 0; i < 7; i++)
And smaller ones too:
#include "stdio.h" should be written #include <stdio.h>
#include "stdafx.h" is not used, and so can be removed - regardless, it should be written as #include <stdafx.h> if it were to be used.
The Results array has size 8, but you only use 7 slots.
main should have prototype int main(void) or int main(int argc, char *argv[]) or equivalent.
favor idiomatic for (i = 0; i < 7; i++) loops over error prone do / while loops.
use braces for a non trivial loop body.
Here is a simpler and better version:
#include <stdio.h>
#include <string.h>
int main(void) {
int Results[7];
int i, n, max;
printf("Enter the results of your 7 leavin cert subjects: ");
for (i = 0; i < 7; i++) {
printf("\nSubject %d: ", i + 1);
if (scanf_s("%d", &Results[i]) != 1) {
printf("invalid number\n");
exit(1);
}
}
for (n = i, max = 0, i = 0; i < n; i++) {
if (Results[i] > max)
max = Results[i];
}
printf("The best grade is %d\n", max);
return 0;
}
Here is an example of what the result should be :
1234 and 1344 Have two numbers on same positions.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Proverka(int *num1[], int *num2[], int len, int pom)
{
if (len < 1 && pom < 1)
return 0;
if (*num1 == *num2)
return 1 + Proverka(num1++, num2++, len-1, pom-1);
else {
return Proverka(num1++, num2++, len-1, pom-1);
}
}
int main (void)
{
int *num1[5], *num2[5], pom, len, i, sin, j;
pom = sizeof(num1) / sizeof(num1[0]);
len = sizeof(num2) / sizeof(num2[0]);
for (i = 0; i < pom; i++) {
printf("Enter elements for first array :");
scanf("%d", num1[i]);
}
for (j = 0; j < len; j++){
printf("Enter elements for second array : ");
scanf("%d", num2[j]);
}
sin = Proverka(num1, num2, pom, len);
{
printf("They have %d numbers on same positions", sin);
}
return 0;
}
You have quite a lot of bugs in the code, here's a working version of it.
Read comments to understand what I changed.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Proverka(int *num1, int *num2, int len1, int len2)
{
if (len1 < 1 && len2 < 1) return 0;
// note the pre-increment. Your post-increment wouldn't have worked. Or just add 1.
return (*num1 == *num2) + Proverka(++num1, ++num2, len1 - 1, len2 - 1);
// this works, since "boolean" is really just 0 or 1
}
int main (void)
{
// I make array of ints, not of pointers to ints.
int num1[5], num2[5], len1, len2, i, same;
len1 = sizeof(num1) / sizeof(num1[0]);
len2 = sizeof(num2) / sizeof(num2[0]);
for (i = 0; i < len1; i++) {
printf("First array element %d:", i+1);
scanf("%d", &num1[i]); // pointer at the element
}
for (i = 0; i < len2; i++){
printf("Second array element %d:", i+1);
scanf("%d", &num2[i]);
}
// get pointers at the first array elements
same = Proverka(&num1[0], &num2[0], len1, len2);
printf("They have %d numbers on same positions\n", same); // newline - good practice
return 0;
}
Here's a bit more "optimized" and cleaned up version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ITEMS 5
int Proverka(const int *aa, const int *bb, const int len)
{
if (len == 0) return 0;
return (*aa == *bb) + Proverka(1 + aa, 1 + bb, len - 1);
}
int main (void)
{
int aa[ITEMS], bb[ITEMS], i, same;
for (i = 0; i < ITEMS; i++) {
printf("First array element %d:", i+1);
scanf("%d", &aa[i]);
}
for (i = 0; i < ITEMS; i++) {
printf("Second array element %d:", i+1);
scanf("%d", &bb[i]);
}
same = Proverka(&aa[0], &bb[0], ITEMS);
printf("They have %d numbers on same positions\n", same);
return 0;
}
Using recursion for this is not a very good choice, a loop would be easier and safer. But this works too - for not-too-large arrays.
int *num1[5],*num2[5] ... doesnt give you an array of integer pointers with memory allocated. Fix that. Allocate memory using malloc.
Or just say int num1[5],num2[5] and scanf("%d",&num1[i]);