I would like to create a char array, print it, reorganize it, and then reprint it in C. Here's what I have so far:
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(){
int i = 0;
int j = 0;
char k[4][2];
char thing[1][1];
strcpy(k[0] , "A");
strcpy(k[1] , "B");
strcpy(k[2] , "C");
strcpy(k[3] , "D");
printf("\nThe original order is: \n");
for (int i = 0; i < 4; i++) { // fill
printf("%s,", k[i]);
}
printf("\nThe reordering is: \n");
for (int i = 0; i < 4; i++) { // reorder
strcpy(thing[0], k[i]);
j = (int)(i + rand() / (RAND_MAX / (5 - i)) );
strcpy(k[i], k[j]);
strcpy(k[j], thing[0]);
printf("%s,", k[i]); // print
}
return(0);
}
Here's my Terminal Output. There are no warnings, just the abort.
mac% clang thing.c -o thing
mac% ./thing
The original order is:
A,B,C,D,
The reordering is:
zsh: abort ./thing
I figured it out! Here's the solution I came up with:
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(){
int i = 0;
int j = 0;
char k[4] = "ABCD";
char thing[1] = "O";
printf("\nThe original order is: \n");
for (int i = 0; i < 4; i++) { // fill
printf("%c,", k[i]);
}
printf("\nThe reordering is: \n");
for (int i = 0; i < 4; i++) { // reorder
thing[0]= k[i];
j = (int)(i + rand() / (RAND_MAX / (4 - i)) );
k[i]= k[j];
k[j]= thing[0];
printf("%c,", k[i]); // print
}
return(0);
}
And the output is
The original order is:
A,B,C,D,
The reordering is:
A,B,D,C,%
Related
I have to convert the binary to string, I tried something, but it didn't work.
This is my code (I don't allowed to change the main function!) , but I can't understand what's wrong:
#include <stdio.h>
#include <stdbool.h>
#include<math.h>
#include<string.h>
void decode_bytes(const int rows, bool bytes[rows][8], char string[rows]){
int iteration = 8;
for(int j = 0; j < rows; j++){
for (int i = 0; i < iteration; i++){
if (bytes[j][i] == 1){
string[j] += pow(2, iteration - (i + 1));
}
}
}
}
int main(){
bool bytes2[7][8] = {
{0,1,0,0,1,0,0,0},
{0,1,1,0,0,1,0,1},
{0,1,1,0,1,1,0,0},
{0,1,1,0,1,1,0,0},
{0,1,1,0,1,1,1,1},
{0,0,1,0,0,0,0,1},
{0,0,0,0,0,0,0,0}
};
char string[7];
decode_bytes(7, bytes2, string);
printf("%s\n", string);
}
Output must be: "hello", but it's "cel<mB"....
Why is this program running fine till a is 5. [as soon as a becomes 6, the program prints its last value (i.e., '000007') and ends without even printing the 'Program END' msg, in the last and without even executing the last getch();] While this program is supposed to work like: print a-1 number of 0 and print 7 at last; this process should repeat itself every time the user enters any character, with an increment in the value of a. I want all of this to happen at the center, an wanted the '7' to be printed seperately, so I used created gotoxy function, and printed the 7 seperately.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
void gotoxy(int x, int y)
{
COORD c;
c.X = x;
c.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
/* X = 0-80
AND Y= 0-25*/
}
int main()
{
int a = 2;
int *ptr;
int k=40;
for (int j = 1; j >= 1; j++)
{
system("cls");
ptr = (int *)calloc(a, sizeof(int));
gotoxy(k, 13);
*(ptr + a) = 7;
// ptr[1] = 0;
for (int i = 1; i < 2; i++)
{
printf("%d",ptr[a]);
}
gotoxy(39, 13);
for (int i = 1; i < a; i++)
{
printf("%d", ptr[i]);
}
a++;
k++;
free(ptr);
getch();
}
printf("\nProgram END");
getch();
return 0;
}
Hello all I want to write a program to randomly choose a character from an array, so far I have come up with this code. I get an Error message help...Thread 1: EXC_BAD_ACCESS (code=1, address=0x48) at the last printf()
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int lower = 1, upper = 6;
int test;
srand((unsigned int)time(0));
char* placeArray[]={"Renti","Mosxato","Tavros","Kallithea","Petralona","Thisio"};
int i, num;
for (i=0;i<6;i++){
num=(rand() % (upper - lower + 1)) + lower;
test=(int)num;
printf("%d ",num);
printf("%s\n",placeArray[num]);
}
return 0;
}
num should be num-1 in the last printf, because the range of the elements in the array is 0 to 5, as shown below
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int lower = 1, upper = 6;
int test;
srand((unsigned int)time(0));
char * placeArray[]= {"Renti", "Mosxato", "Tavros", "Kallithea", "Petralona", "Thisio"};
int i, num;
for (i = 0; i < 6; i++){
num = (rand() % (upper - lower + 1)) + lower;
test = (int)num;
printf("%d ", num);
printf("%s\n", placeArray[num - 1]);
}
return 0;
}
#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;
}
I updated my main and sequetialSearch and now it crashes when it runs. It compiles okay, but then crashes.
main.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include "percentage.h"
#include "sequentialSearch.h"
#define searchAmount 100
int main(int argc, char *argv[])
{
int numbers[100];
int searches[searchAmount];
int testAmounts[searchAmount];
int i;
int where;
int searchSuccess;
int searchUnsuccess;
int percent;
int looker;
int sum;
int average;
srand(time(NULL));
for (i = 0; i < 100; i++){
numbers[i] = rand() % 200;
}
for (i = 0; i < searchAmount; i++){
searches[i] = rand() % 200;
}
searchUnsuccess = 0;
searchSuccess = 0;
sum = 0;
for(i = 0; i < searchAmount; i++){
if(seqSearch(numbers, 100, searches[i], &where, &looker)){
searchSuccess++;
testAmounts[i] = looker;
}else{
searchUnsuccess++;
testAmounts[i] = looker;
}
}
for(i = 0; i < searchAmount; i++){
sum = sum + testAmounts[i];
}
average = sum / searchAmount;
percent = percentRate(searchSuccess, searchAmount);
printf("Total number of searches: %d\n", searchAmount);
printf("Total successful searches: %d\n", searchSuccess);
printf("Success Rate: %d%%\n", percent);
printf("Total number of tests ran: %d\n", average);
system("PAUSE");
return 0;
}
sequentialSearch.h
bool seqSearch (int list[], int last, int target, int* locn, int* looker){
*looker = 0;
while(*looker < last && target != list[*looker]){
*looker++;
}
*locn = *looker;
return(target == list[*looker]);
}
Pass looker in by reference, so that you can access its value from the caller.
int looker;
...
for(i = 0; i < searchAmount; i++){
if(seqSearch(numbers, 100, searches[i], &where, &looker)){
searches[i] += looker;
searchSuccess++;
}else{
searchUnsuccess++;
}
}
bool seqSearch (int list[], int last, int target, int* locn, int *looker){
*looker = 0;
while(*looker < last && target != list[*looker]){
(*looker)++;
}
*locn = *looker;
return(target == list[*looker]);
}
By the way, you may wish to reconsider defining functions in your header file; this could cause problems with duplicate symbol when linking if you have more than one c file including this file.
Why not just pass looker in as an int*, use it essentially as you have been, look at the value after seqSearch(...) returns, and add it to a running total back in main()?
One problem is that the increment of looker in seqSearch is incrementing the pointer rather than the value. It should probably be:
(*looker)++;