Code Monkey home page Code Monkey logo

problemstatements-c-cpp's Introduction

C-CPP-Problem-Statements

I have tried to solve some of the problems that I have faced in C-CPP development to learn in deep concepts.

Setting up Visual Studio Code for C C++ Development

If you have not setup your C CPP Development Environment then this will help you.

Download MinGW by following Download tab. Install all the packages that MinGW Offers. For reference follow this link.

Problem 1

Write a parallel program using pthread to accomplish the same as the following serial program. For the following serial program, the user enters a line and the program outputs the number of occurrences of each character. For the parallel program if we have n threads we should divide the line into n segments and let each thread counts the characters of its segment.

Note: Do not count the space or the tab characters.

//File name: a.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ASCIIs		127		//ASCII characters from 0 to 127
#define atmost		1000


char letters[atmost + 1];	//Extra location for the string terminator '\0'
int count[ASCIIs];

int main(){
  int i, index;

  for(i = 0; i < ASCIIs; i++)
    count[i] = 0;

  printf("Enter a line not larger than 100 characters.\n");
  fgets(letters, atmost+1, stdin);//Read a line into array letters. This function adds '\0' to the end of the string

  for(i = 0; i < strlen(letters); i++){
    index = (int)letters[i];
    (count[index])++;
  }

  for(i = 33; i <  ASCIIs; i++)
    if(count[i] != 0)
      printf("Number of %c is: %d\n", i, count[i]); 
  return 0;
}

For example:

Enter a line not larger than 100 characters.
for(i = 0; i < strlen(letters); i++){
Number of ( is: 2
Number of ) is: 2
Number of + is: 2
Number of 0 is: 1
Number of ; is: 2
Number of < is: 1
Number of = is: 1
Number of e is: 3
Number of f is: 1
Number of i is: 3
Number of l is: 2
Number of n is: 1
Number of o is: 1
Number of r is: 3
Number of s is: 2
Number of t is: 3
Number of { is: 1

The boldfaced line is what the user entered.

For solution run solutions/problem1.cpp

problemstatements-c-cpp's People

Contributors

abhinav-jha-dev avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.